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

题目描述:

There is a fishing game as following:
-------The game contains nn stages, numbered from 11 to nn.
-------There are four types of stages (numbered from 00 to 33) type 00: There are no fish and no clam in this stage. type 11: There are no
fish and one clam in this stage. type 22: There are one fish and no
clam in this stage. type 33: There are one fish and one clam in this
stage. In each stage, you can do exactly one of the following four
actions.

  1. If there is a clam in the stage, you can use this clam to make one pack of fish bait. And the number of packs of fish bait you have is
    increased by one. You can use this pack of fish bait to catch fish
    after this stage.
  2. If there is one fish in the stage, you can catch this fish without any fish bait. After this stage, the number of packs of fish bait you
    have is not changed.
  3. If you have at least one pack of fish bait. You can always catch one fish by using exactly one pack of fish bait even if there are no
    fish in this stage. After this stage, the number of packs of fish bait
    you have is decreased by one.
  4. You can do nothing.

Now, you are given nn and the type of each stage. Please calculate the
largest number of fish you can get in the fishing game.

输入描述:

2020牛客暑期多校训练营(第三场)------ Clam and Fish_第1张图片

输出描述:

在这里插入图片描述

示例

输入
2
4
0103
1
1
输出
2
0

备注:

One possible scenario you can catch two fishes is as follow:

stage 1: Do nothing.
stage 2: Make a pack of fish bait.
stage 3: Catcha fish by a pack of fish bait.
stage 4: Catch the fish that appears in the stage.

题意:

有n个水池
水池有四种类型:
1.0代表水池没有鱼和没有蛤蜊
2.1代表水池没有鱼但有蛤蜊
3.2代表有一条鱼但没有蛤蜊
4.3代表有鱼有蛤蜊

在每个水池中,你只能选择下面操作中的一种:
1.如果有蛤蜊,你可以捉蛤蜊做成诱饵
2.如果有鱼,你可以捕鱼,并且捕鱼不消耗诱饵
3.如果没有鱼,但是你有诱饵,你可以捕鱼,并消耗一包诱饵
4.什么都不做

问你最多能捕几条鱼?

经过分析我们可以知道水池有鱼的时候我们可以进行捕鱼操作,即使水池有鱼和蛤蜊,如果捉蛤蜊,你将会需要至少两个水池才能捕一条鱼,但是如果进行捕鱼,那么只会消耗一个水池,因此在有鱼的时候捕鱼是最优的。
然后我们可以把字符串从左到右进行遍历,设置一个变量count,在只有蛤蜊的情况下,count++,如果水池什么都没有,在count不为0的时候,count–,鱼的数量+1,遇到鱼则捕鱼。
在最后如果count还有剩余,说明在有些有蛤蜊的水池中我们可以进行捕鱼操作 鱼的数量+(count/2)

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f
#include

using namespace std;
typedef long long ll;
typedef pair<int,int>P;

char s[2000010];
int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        int n;scanf("%d%s",&n,s);
        int ans=0,cnt=0;
        for(int i=0;i<n;i++)
        {
            if(s[i]=='0'&&cnt>0)
            {
                cnt--;ans++;
            }
            else if(s[i]=='1')cnt++;
            else if(s[i]=='2')ans++;
            else if(s[i]=='3')ans++;
        }
        if(cnt)ans+=cnt/2;
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(2020牛客暑期多校训练营(第三场)------ Clam and Fish)