题面:
题意:
有 n 天,每天鱼塘里有四种情况:
有鱼,有蛤蜊
有鱼,无蛤蜊
无鱼,有蛤蜊
无鱼,无蛤蜊
之后你每天都可以执行以下的一种操作:
若有鱼,则可以钓鱼
若有蛤蜊,则可以收集蛤蜊
若手头有多余蛤蜊,则可以用一个蛤蜊换一条鱼
可以什么也不干
问 n 天之后你能获得的最多的鱼数。
题解:
贪心。
如果当前天有鱼一定钓鱼。
如果当前天有蛤蜊,就收集蛤蜊。
如果当前天什么也没有且手里有蛤蜊就拿蛤蜊换一条鱼。
最后手里如果还剩下n个蛤蜊,那么可以分出一半拿蛤蜊的天数来,用剩下的那一半蛤蜊在这些天换成鱼。
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<bitset>
#include<map>
#include<unordered_map>
#include<set>
#define ui unsigned int
#define ll long long
#define llu unsigned ll
#define ld long double
#define pr make_pair
#define pb push_back
#define lc (cnt<<1)
#define rc (cnt<<1|1)
#define len(x) (t[(x)].r-t[(x)].l+1)
#define tmid ((l+r)>>1)
using namespace std;
const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const double dnf=1e18;
const int mod=998244353;
const double eps=1e-8;
const double pi=acos(-1.0);
const int hp=13331;
const int maxn=2000100;
const int maxp=1100;
const int maxm=100100;
const int up=100000;
char str[maxn];
char op[10];
int main(void)
{
int tt;
scanf("%d",&tt);
while(tt--)
{
int n;
scanf("%d%s",&n,str);
int ans=0;
int cnt=0;
for(int i=0;i<n;i++)
{
if(str[i]=='2'||str[i]=='3') ans++;
else
{
if(str[i]=='1') cnt++;
else if(str[i]=='0')
{
cnt--;
if(cnt>=0) ans++;
else cnt=0;
}
}
}
printf("%d\n",ans+cnt/2);
}
return 0;
}