2020牛客暑期多校训练营(第九场)

A.Groundhog and 2-Power Representation
py还是香,还得练练py代码的能力

eval是可以直接把这个输入的字符串当初数值表达式计算,所以我们把所有的左括号改为 ’ **( ’ 即可,因为 **在py中是乘方的意思

print(eval(input().replace('(', '**(')))

F.Groundhog Looking Dowdy
思路:
把每一个数从小到大排序并标好其属于哪一天的。
然后用尺取法来解决(不断更新最小值),时间复杂度O(n)

AC代码

#include 
inline int read(){char c = getchar();int x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 1e6 + 5;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const int mod = 1e9+7;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
vector<pii> v;
int vis[N];
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //    freopen("input.txt","r",stdin);
    //    freopen("output.txt","w",stdout);
    int n,m,k;
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i++)
    {
        scanf("%d",&k);
        for(int j = 1,num;j <= k;j++)
        {
            scanf("%d",&num);
            v.push_back({num,i});
        }
    }
    sort(v.begin(),v.end());
    int sum = 0,ans = INF,l = 0;
    for(int i = 0;i < v.size();i++)
    {
        if(!vis[v[i].second]) sum++;
        vis[v[i].second]++;
        while(sum > m)//尺取法
            vis[v[l].second]--,l++,sum--;
        if(sum == m)
            ans = min(ans,v[i].first-v[l].first);
    }
    printf("%d\n",ans);
}

I.The Crime-solving Plan of Groundhog
同样是py代码

t = int(input())
for it in range(0, t):
    n = int(input())
    datanum = list(map(int, input().split()))
    datanum = sorted(datanum)
    Min = 0
    for its in range(0, n):
        if datanum[its] != 0:
            Min = its
            break
    a = datanum[Min]
    b = datanum[Min + 1]
    datanum[Min] = datanum[Min + 1] = -1
    b = int(str(b) + ''.join([str(itss) for itss in datanum if itss != -1]))
    print(a * b)

先拖着叭,把那几个题补了,回头更新

你可能感兴趣的:(牛客)