Codeforces Gym 100269E Energy Tycoon 贪心

题目链接:http://codeforces.com/gym/100269/attachments

题意:

有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去

每个东西在每秒钟都会产生1的能力。

然后问你怎么放才能使得最后能力最大,输出出来

解法:

贪心,最后肯定1越多越好

所以我们放1的时候,注意一下,如果放不下的话,就把其中一个2扔掉,然后放1就好了

//CF gym 100269E

#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int main(){
    freopen("energy.in","r",stdin);
    freopen("energy.out","w",stdout);
    cin >> n;
    cin >> s;
    long long a1 = 0, a2 = 0, ans = 0;
    for(int i = 0; s[i]; i++){
        if(s[i] == '1'){
            if(a1+a2*2+1<=n) a1++;
            else if(a2){
                a2--;
                a1++;
            }
        }
        if(s[i] == '2'){
            if(a1+a2*2+2<=n){
                a2++;
            }
        }
        ans+=(a1+a2);
    }
    cout << ans << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/spfa/p/6594690.html