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

题目链接

题目描述

There is a fishing game as following:

The game contains nn stages, numbered from 1 to n.

There are four types of stages (numbered from 0 to 3):

type 0: There are no fish and no clam in this stage.

type 1: There are no fish and one clam in this stage.

type 2: There are one fish and no clam in this stage.

type 3: 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≤t≤2.5×105t (1 \le t \le 2.5 \times 10^5t(1≤t≤2.5×105) — the number of test cases.

There are two lines in each test. The first line contains one integer n(1≤n≤2×106)n (1 \le n \le 2 \times 10^6)n(1≤n≤2×106), 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×1062 \times 10^62×106 .

输出描述:

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

思维题,我们考虑如何获得捕获最多的鱼:
1.对 000,有饵就捕鱼
2.对 222 或 333,直接选择捕鱼,不浪费鱼饵
3.对 111,可以捕鱼也可以做鱼饵,我比赛时是判断该位置后面 000 的数量来看选择哪一种的,但是看了别人的代码好像更加简单,就是默认制饵,最后加上剩下的鱼饵数除 222 就是答案,可以当作一半用来制饵,一半用来捕鱼。
AC代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e6+5;
int main(){int n,t;char s[N];scanf("%d",&t);while(t--){scanf("%d%s",&n,s);int ans=0,sum=0;for(int i=0;i<n;i++){if(s[i]=='0'){if(sum) sum--,ans++;}else if(s[i]=='1') sum++;else ans++;}printf("%d\n",ans+sum/2);}return 0;
}

2020牛客暑期多校训练营(第三场)A.Clam and Fish相关推荐

  1. 2020牛客暑期多校训练营(第一场)

    文章目录 A B-Suffix Array B Infinite Tree C Domino D Quadratic Form E Counting Spanning Trees F Infinite ...

  2. 2020牛客暑期多校训练营(第二场)

    2020牛客暑期多校训练营(第二场) 最烦英语题 文章目录 A All with Pairs B Boundary C Cover the Tree D Duration E Exclusive OR ...

  3. E Groundhog Chasing Death(2020牛客暑期多校训练营(第九场))(思维+费马小定理+质因子分解)

    E Groundhog Chasing Death(2020牛客暑期多校训练营(第九场))(思维+费马小定理+质因子分解) 链接:https://ac.nowcoder.com/acm/contest ...

  4. 2020牛客暑期多校训练营(第一场)A B-Suffix Array(后缀数组,思维)

    链接:https://ac.nowcoder.com/acm/contest/5666/A 来源:牛客网 题目描述 The BBB-function B(t1t2-tk)=b1b2-bkB(t_1 t ...

  5. 2019牛客暑期多校训练营 第三场 I Median

    传送门 链接:https://ac.nowcoder.com/acm/contest/883/I 来源:牛客网 JSB has an integer sequence a1,a2,-,ana_1, a ...

  6. 2020牛客暑期多校训练营(第二场)Just Shuffle

    https://ac.nowcoder.com/acm/contest/5667/J 题目大意:给你一个置换A,使得置换P^k=A,让你求出置换P. 思路:我们根据置换A再置换z次,那么就等于置换p ...

  7. 2020牛客暑期多校训练营(第一场)j-Easy Integration(思维,分数取模,沃斯利积分)

    题目链接 题意: 给你一个积分公式,给你一个n,问积分公式的值取模后的结果. 思路: 积分公式(沃利斯积分)值的结论直接就是(n!)^2/(2n+1)!,求个阶乘,再用费马小定理给1/(2n+1)!取 ...

  8. 2020 牛客暑期多校训练营(第一场)F

    题目大意: 多次输入两个a,b字符串他们可以无限次的重复变成aaa,或者bbb 比较他们的大小,相同输出 =,a<b输出 <,a>b输出 >. 输入: aa b zzz zz ...

  9. 2020牛客暑期多校训练营(第二场)未完待续......

    F. Fake Maxpooling 题目: 题目大意: 输入n,m,k.矩阵的尺寸为nm,其中每一个元素为A[i][j] = lcm( i , j ).从中找出所有kk的子矩阵中元素最大的数之和. ...

  10. 2020牛客暑期多校训练营(第一场)J、Easy Integration (数学、分部积分)

    题目链接 题面: 题意: 求给定的定积分. 题解,化成 ∫ xn (1-x)n dx 然后用分部积分法即可得. 分部积分法:∫ udv = uv - ∫ vdu 最终为 n!/((n+1)*(n+2) ...

最新文章

  1. VTK:Procrustes 对齐过滤器用法实战
  2. 从响应式Spring Data存储库流式传输实时更新
  3. [react] React的render中可以写{if else}这样的判断吗?
  4. 会话管理-1.1.Cookie介绍
  5. linux内核中cent文件夹,Centos 中如何快速定制二进制的内核 RPM 包
  6. java base64字符 转图片_JAVA实现图片与base64字符串之间的转换详解
  7. C++: find()函数的注意事项
  8. python读音-Python怎么读
  9. [转载] python之numpy的基本使用
  10. zabbix3.2短信告警脚本
  11. 各国语言中的“你好”
  12. springbus类是做什么用的_SpringCloud-Bus组件的使用
  13. python将图片变成水墨画
  14. css3 书页卷脚_css3实现的书本立体翻页效果代码实例
  15. 公积金查询,公积金账号查询
  16. ConcurrentHashMap比其他并发集合的安全效率要高一些?
  17. 几款免费wordpress主题推荐
  18. MyBatis在Spring中的事务管理
  19. dsp调音一次多少钱_dsp调音技巧
  20. Xilinx ISE系列教程(8):读取FPGA芯片唯一ID号

热门文章

  1. 【Microsoft Azure 的1024种玩法】二十七. Azure Virtual Desktop虚拟桌面之快速创建配置(一)
  2. superset、sqlalchemy链接数据库报错 ‘SSL connection error: error:1425F102:SSL routines:ssl_choose_client_vers
  3. 初等函数积分的刘维尔定理Liouville's theorem on integration in terms of elementary functions
  4. CorelDRAW2022订阅版本专属功能
  5. javascript DOM小结
  6. 解决 strict-origin-when-cross-origin 问题
  7. 学习 React 17 系统精讲 结合TS打造旅游电商平台
  8. 【阿朱标红】投资创业型人才 公司从平台化到社会化
  9. restart和reload的区别
  10. 阮一峰:美丽心灵的永恒阳光