2020牛客暑期多校训练营(第三场)A Clam and Fish

2020牛客暑期多校训练营(第三场)A Clam and Fish

题解:

  • 如果有鱼和鱼饵,那就钓鱼
  • 如果只有鱼饵,那么就做鱼饵

最后的状态可能是鱼饵多了 \(x\),那么答案就是多余的鱼饵数量 \(x/2\)

#include <bits/stdc++.h>
#define debug(x) printf("debug:%s=%lld\n",#x,x);
//#define debug(x) cout << #x << ": " << x << endl
using namespace std;
const int maxn = 2e6+10;
typedef long long ll;
char s[maxn];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,ans=0,res=0;
        scanf("%d",&n);
        scanf("%s",s+1);
        for(int i=1;i<=n;i++){
            int x = s[i]-'0';
            if(x==3||x==2) ans++;
            else if(x==1) res++;
            else if(res) res--,ans++;
        }
        ans+=res/2;
        printf("%d\n",ans);
    }
    return 0;
}
posted @ 2020-07-20 21:00  EchoZQN  阅读(94)  评论(0编辑  收藏  举报