Codeforces 1154D - Walking Robot - [贪心]

题目链接:https://codeforces.com/contest/1154/problem/D

 

题解:

贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走。有太阳的时候,如果可充电电池能够充一格电,就用普通电池跑(让可充电池充电),否则就用可充电电池走。

 

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;

int n,a,b;
bool s[maxn];

int main()
{
    cin>>n>>a>>b;
    for(int i=1;i<=n;i++) scanf("%d",&s[i]);
    int cnt=0, x=a, y=b;
    for(int i=1;i<=n;i++)
    {
        if(s[i]==0) //no sun
        {
            if(y>0) y--, cnt++;
            else if(x>0) x--, cnt++;
            else break;
        }
        else
        {
            if(x>0 && y<b) x--, y++, cnt++;
            else if(y>0) y--, cnt++;
            else break;
        }
    }
    cout<<cnt<<endl;
}

 

posted @ 2019-04-20 19:19  Dilthey  阅读(535)  评论(0编辑  收藏  举报