2020牛客多校 第三场 A-Clam and Fish (贪心 + 模拟)

题目链接: A-Clam and Fish

Description

题意:给出四种状态,可以执行四种操作,求获取的最大鱼的数量。

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.

Sample Input

2
4
0103
1
1

Sample Output

2
0

More Info

  • 1 <= t <= 2.5 × 105
  • 1 <= n <= 2 × 106

Method

贪心

  • 从左至右选择操作:
    • 如果有鱼,则直接抓鱼;
    • 如果只有鱼饵,做鱼饵;
    • 如果什么都没有同时有已经做好的鱼饵, 花一个鱼饵钓鱼;
  • 最后判断已做好的鱼饵,如果鱼饵数大于1,则将制作这些剩余鱼饵一半的时间用来花鱼饵钓鱼,即sum += bait/2;
  • sum 即为所求。

当然也可以反着求解,一个道理处理好做鱼饵和花鱼饵的时间就行

Code

详见注释

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std; 
#define ll long long
#define Max 2000005 

int main()
{
	int t, n;
	scanf("%d", &t);
	while(t--)
	{
		char s[Max];										//记录状态 
		ll sum=0, bait=0;									//最大钓鱼数,鱼饵数 
		scanf("%d%s", &n, s);
		int len = strlen(s);
		for(int i=0; i<len; i++)
		{
			if(s[i] == '2'||s[i] == '3') sum++;				//如果有鱼就钓鱼 
			else if(s[i] == '1') bait++; 					//如果只有鱼饵,就做鱼饵 
			else if(s[i] == '0'&&bait > 0) bait--,sum++;	//如果什么都没有且有鱼饵,则花费一个鱼饵吊一条鱼 
		}
		sum += bait/2;										//如果鱼饵有多,则将做bait/2鱼饵的时间用来花鱼饵换鱼 
		printf("%lld\n", sum);								//输出钓鱼最大条数 
	}
	return 0;
}

蒟蒻一只,欢迎指正

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值