题目
n个池子,编号1n,分四种,编号03
第0种:0f0c 1:0f1c 2:1f0c 3:1f1c
每个池子动一步:
1.有1c,则用它做一包鱼饵,储存在后面钓鱼
2.有1f,则空手抓鱼
3.有鱼饵,则可以在没鱼的池子里搞一条,花一个鱼饵
4.啥也没干
输入:t(样例数)每个样例两行,第一行n,第二行是一个字符串
(n的总和小于2e6)
输出:最大的鱼数
代码
#include<bits/stdc++.h>
using namespace std;
int t,n,x,i,j,tot;
int main()
{
string s;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
cin>>s;
tot=x=i=0;
bool flag=0;
while(s[i]=='0') i++;
for(;i<n;i++)
{
if(s[i]=='2' || s[i]=='3')
{
tot++;
continue;
}
if(flag==0 && s[i]=='1') flag=1;
if(flag==1 && s[i]=='1') x++;
if(flag==1 && s[i]=='0' && x) tot++,x--;
}
if(x>=2) tot+=x/2;
printf("%d\n",tot);
}
return 0;
}
思路
1.在类型3与4时,直接抓鱼永远是最佳选择;
在类型0时,只能通过鱼饵抓鱼;
类型1时,可以换鱼饵或者用鱼饵抓鱼。
2.默认能做鱼饵就做鱼饵,到最后通过鱼饵换鱼,两个多的鱼饵换一条。