Codeforces Round #552 (Div. 3)D. Walking Robot【贪心】

思路:如果当前有阳光,并且蓄电池不是饱和情况,我们就让蓄电池充电,如果蓄电池饱和我们就先用蓄电池,这样下次才有机会充电,总的来说就是贪心的思想。


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1|1
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
int s[maxn];

int main()
{
    int n, b, a;
    scanf("%d%d%d", &n, &b, &a);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &s[i]);
    int ans = 0;
    int tmp = a;
    for(int i = 1; i <= n; ++i)
    {
        if(s[i] == 0 && a > 0)
        {
            ++ans;
            --a;
        }
        else if(s[i] == 0 && b > 0)
        {
            ++ans;
            --b;
        }
        else if(s[i] == 1)
        {
            if(a == tmp)
            {
                --a;
                ++ans;
            }
            else if(b > 0)
            {
                ++a;
                a = min(a, tmp);
                ++ans;
                --b;
            }
            else if(a > 0)
            {
                --a;
                ++ans;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41785863/article/details/89386111