CodeForces - 962B:Students in Railway Carriage(贪心)

39 篇文章 1 订阅

链接https://vjudge.net/problem/CodeForces-962B

题目
There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger.

The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that:

  • no student-programmer is sitting next to the student-programmer;
  • and no student-athlete is sitting next to the student-athlete.

In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting.

Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all).

题意
一群程序员(A类人群)和一群运动员(B类人群)一起坐地铁。
现在地铁上有n个位置,.代表空位,*代表有人坐。
众所周知,一山不容二虎,除非一公一母(doge)。程序员不想和程序员挨着,运动员也是一样。

在这里插入图片描述

现在他们想你求助,问你最多能有多少人搭乘地铁(看来他们并不想站着)?

思路
一开始没看懂题意,不过很快就get到点了。
这里我们贪心走一波就好了,每次都优先给人数多的那一方找座位.
从前向后遍历字符串,如果有座位先考虑人数多的一方能不能在这安排一个人,如果不行再看另一方有没有人。
如果另一方没人了就让仅剩的那一方隔着一个位置坐(例如A.A.A)。

废话不多说,伊丽莎白!!

在这里插入图片描述
代码

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n,s1,s2,k=0;
    string ss;
    cin>>n>>s1>>s2;
    cin>>ss;
    for(int i=0; i<n; i++)
    {
        if(ss[i]=='.')
        {
            if(s1>=s2)            //看一下哪边人多
            {
                if(ss[i-1]!='A'&&s1)
                    s1--,k++,ss[i]='A';
                else if(ss[i-1]!='B'&&s2)
                    s2--,k++,ss[i]='B';
            }
            else
            {
                if(ss[i-1]!='B'&&s2)
                    s2--,k++,ss[i]='B';
                else if(ss[i-1]!='A'&&s1)
                    s1--,k++,ss[i]='A';
            }
        }
    }
    cout<<k<<endl;
}

又是舒适的一天,溜了溜了~~

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值