牛客多校第三场A【Clam and fish】贪心

A【Clam and fish】贪心

链接:https://ac.nowcoder.com/acm/contest/5668/A
来源:牛客网
题目:
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.

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.

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.

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.

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.

输入描述:
The first line contains one integer t (1 \le t \le 2.5 \times 10^51≤t≤2.5×10
5
) — the number of test cases.

There are two lines in each test. The first line contains one integer n (1 \le n \le 2 \times 10^61≤n≤2×10
6
), indicating the number of stages in this game. The second line contains a string with length n. The i-th character of this string indicates the type of the i-th stage.

The sum of n across the test cases doesn’t exceed 2 \times 10^62×10
6
.
输出描述:
For each test case print exactly one integer — the maximum number of fish you can catch in the game configuration.
示例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: Catch a fish by a pack of fish bait.
stage 4: Catch the fish that appears in the stage.

总结:有鱼就抓(之前看直播讲题的时候这句话印象超深刻)
就是2 3这两种情况,直接fish++就好,碰到0,有饵就fish++,碰到1就bait++。最后sum=fish+bait/2;(/2的意义就是:举个例子,11两次,一次制饵一次钓鱼是获得🐟最多的方式,所以以此类推便可得/2)

最开始写的是自定义函数不知道为什么过不了,要是有大佬用自定义函数过了麻烦告诉我一波。

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;

typedef long long ll;


int main()
{
    ll k;
    cin>>k;
    while(k--)
    {
        ll x;
        char s[200006];
        scanf("%lld%s",&x,s);
        ll sum=0,fish=0,bait=0;
        for(int i=0;i<x;i++)
        {

            if(s[i]=='0')
                {
                if(bait>0)
                    {
                        fish++;
                        bait--;
                    }
                }
            else if(s[i]=='1')
            {
                bait++;
            }
            else fish++;
            //cout<<fish;
        }
        sum+=fish+bait/2;
        cout<<sum<<endl;
    }
   return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值