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

题目链接

题面:
在这里插入图片描述

题意:
有 n 天,每天鱼塘里有四种情况:
有鱼,有蛤蜊
有鱼,无蛤蜊
无鱼,有蛤蜊
无鱼,无蛤蜊

之后你每天都可以执行以下的一种操作:
若有鱼,则可以钓鱼
若有蛤蜊,则可以收集蛤蜊
若手头有多余蛤蜊,则可以用一个蛤蜊换一条鱼
可以什么也不干

问 n 天之后你能获得的最多的鱼数。

题解:
贪心。
如果当前天有鱼一定钓鱼。
如果当前天有蛤蜊,就收集蛤蜊。
如果当前天什么也没有且手里有蛤蜊就拿蛤蜊换一条鱼。

最后手里如果还剩下n个蛤蜊,那么可以分出一半拿蛤蜊的天数来,用剩下的那一半蛤蜊在这些天换成鱼。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<bitset>
#include<map>
#include<unordered_map>
#include<set>
#define ui unsigned int
#define ll long long
#define llu unsigned ll
#define ld long double
#define pr make_pair
#define pb push_back
#define lc (cnt<<1)
#define rc (cnt<<1|1)
#define len(x)  (t[(x)].r-t[(x)].l+1)
#define tmid ((l+r)>>1)
using namespace std;

const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const double dnf=1e18;
const int mod=998244353;
const double eps=1e-8;
const double pi=acos(-1.0);
const int hp=13331;
const int maxn=2000100;
const int maxp=1100;
const int maxm=100100;
const int up=100000;

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



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值