Codeforces Round #407 (Div. 2)

来源:互联网 发布:星星知多少的自频道 编辑:程序博客网 时间:2024/06/06 01:31

A - Anastasia and pebbles(水题)

题意:

  一个人有俩口袋,每个口袋最多装k个,然后每天每个口袋里的石头颜色必须都相同,问你最少用几天能装完。

思路:

  水题。。一不小心写瓷了,以为A题大水题,瞎写也不会T,没仔细想。。写了一个T的代码。

#include  <bits/stdc++.h>using namespace std;int main(){    int n, k;    cin >> n >> k;    vector<int>vec;    for(int i = 1; i <= n; i++)    {        int x;        scanf("%d", &x);        vec.push_back(x);    }    int ans = 0;    for(auto o : vec)    {        ans += (o + k - 1) / k;    }    cout << (ans + 1) / 2 <<endl;    return 0;}


 

B - Masha and geometric depression(模拟)

题意:

  给你一个等差数列的首项b1和公比q,还有一个上界l,但是b1和q都可以为0。又给出一个序列a,然后挨个计算出这个等差数列bi,如果bi出现在序列a中,那么就不写出来,最后问你写出来的数有几个。前提是所有的bi的绝对值都必须不超过上界l。

思路:

  一开始写了一个分类讨论的,比较麻烦。。容易写wa。

        后来发现这种题都是可以暴力check的,暴力一个范围然后看有没有,如果还有数,INF。

#include  <bits/stdc++.h>using namespace std;typedef long  long LL;int main(){    int b1, q, l, m;    cin >> b1 >>q >> l >> m;    set<LL>s;    for(int i = 0; i < m; i++)    {        LL x;        cin >> x;        s.insert(x);    }    if(abs(b1) > l)    {        puts("0");        return 0;    }    if(b1 == 0)    {        int haveb1 = (s.find(b1) == s.end());        if(haveb1)  puts("inf");        else puts("0");    }    else if(q == 0)    {        LL haveb1 = (s.find(b1) == s.end());        LL have0 = (s.find(0) == s.end());        if(have0) puts("inf");        else if(haveb1) puts("1");        else puts("0");    }    else if(q == 1)    {        int haveb1 = (s.find(b1) == s.end());        if(haveb1)puts("inf");        else printf("0\n");    }    else if(q == -1)    {        int haveb1 = (s.find(b1) == s.end());        int havefub1 = (s.find(-b1) == s.end());        if(haveb1 || havefub1)  puts("inf");        else printf("0\n");    }    else    {        LL ans = 0;        for(LL i = b1; abs(i) <= l; i *= q)        {            if(s.find(i) == s.end()) ans++;        }        cout << ans << endl;    }    return 0;}



C - Functions again(最大字段和)

题意:

  给你这么一个公式,http://codeforces.com/predownloaded/46/60/4660016cb51fe73726a0e0024bd02d214c153792.png?_=6645764,输出f的最大值。

数据:

   (2 ≤ n ≤ 105)(-109 ≤ ai ≤ 109)

思路:

  拿一两个样例比划一下,就能明白,其实就是最大字段和,扫两边,翻转一下正负号就好了。

#include <bits/stdc++.h>using namespace std;typedef long  long LL;LL a[100000 + 5];LL d[100000 + 5];int n;LL solve(){    LL maxx = -(1LL << 60);    LL temp = 0;    for(int i =0; i < n - 1; i++)    {        if(temp < 0)    temp = d[i];        else temp += d[i];        maxx = max(maxx, temp);    }    return maxx ;}int main(){    cin >> n;    for(int i = 0; i < n; i++)    {        cin >> a[i];    }    for(int i = 0; i < n; i++)    {        d[i] = abs(a[i + 1] - a[i]);        if(i % 2 == 0)  d[i] *= -1;    }    LL maxx1 = solve();    for(int i = 0; i < n; i++)    {        d[i] *= -1;    }    LL maxx2 = solve();    cout << max(maxx1, maxx2) << endl;    return 0;}


 

D - Weird journey

 

 

 

E - The Great Mixing

题意:

  给你一个数n,一个数k,再给k个数字,每个数字可以选择任意个数,最后问你最少使用“几个次”数字使得平均数为n。

数据:

   (0 ≤ n ≤ 1000, 1 ≤ k ≤ 106) ,(0 ≤ ai ≤ 1000)

思路:

  先统一一下度量,这k个数字ai,对最后结果的贡献应该是ai-n吧,所以先这么处理一下。然后bfs就行了。因为最多只有-1000到1000的范围,而k个数字实际上,只有最多有1000种。所以大概是1k*2k的样子。所以bfs没啥问题。

#include <bits/stdc++.h>using namespace std;typedef pair<int, int>pii;int n, k;int step[2005];queue<int>que;set<int>vec;int solve(){    while(que.size())    {        int val = que.front();que.pop();        if(val == 0)    return step[val + 1000];        for(auto o : vec)        {            int fval = val + o;            if(fval >= -1000 && fval <= 1000 && step[fval + 1000] == 0)            {                que.push(fval);                step[fval + 1000] = step[val + 1000] + 1;            }        }    }    return -1;}int main(){    scanf("%d%d", &n, &k);    for(int i = 0; i < k; i ++)    {        int x;        scanf("%d", &x);        x -= n;        vec.insert(x);        que.push(x);        step[x + 1000] = 1;    }    printf("%d\n", solve());    return 0;}





0 0
原创粉丝点击