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

本文深入解析了牛客网上的Clamandfish贪心算法问题,通过详细解释游戏规则和策略,展示了如何计算在给定阶段类型下能够捕获的最大鱼数。文章提供了代码实现,并附带了示例输入输出,帮助读者理解算法的运作过程。

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;

}

"Mstar Bin Tool"是一款专门针对Mstar系列芯片开发的固件处理软件,主要用于智能电视及相关电子设备的系统维护与深度定制。该工具包特别标注了"LETV USB SCRIPT"模块,表明其对乐视品牌设备具有兼容性,能够通过USB通信协议执行固件读写操作。作为一款专业的固件编辑器,它允许技术人员对Mstar芯片的底层二进制文件进行解析、修改与重构,从而实现系统功能的调整、性能优化或故障修复。 工具包中的核心组件包括固件编译环境、设备通信脚本、操作界面及技术文档等。其中"letv_usb_script"是一套针对乐视设备的自动化操作程序,可指导用户完成固件烧录全过程。而"mstar_bin"模块则专门处理芯片的二进制数据文件,支持固件版本的升级、降级或个性化定制。工具采用7-Zip压缩格式封装,用户需先使用解压软件提取文件内容。 操作前需确认目标设备采用Mstar芯片架构并具备完好的USB接口。建议预先备份设备原始固件作为恢复保障。通过编辑器修改固件参数时,可调整系统配置、增删功能模块或修复已知缺陷。执行刷机操作时需严格遵循脚本指示的步骤顺序,保持设备供电稳定,避免中断导致硬件损坏。该工具适用于具备嵌入式系统知识的开发人员或高级用户,在进行设备定制化开发、系统调试或维护修复时使用。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值