The Festive Evening (Codeforces - 834B)

题目链接:

http://codeforces.com/problemset/problem/834/B

题目:

B. The Festive Evening
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

It's the end of July – the time when a festive evening is held at Jelly Castle! Guests from all over the kingdom gather here to discuss new trends in the world of confectionery. Yet some of the things discussed here are not supposed to be disclosed to the general public: the information can cause discord in the kingdom of Sweetland in case it turns out to reach the wrong hands. So it's a necessity to not let any uninvited guests in.

There are 26 entrances in Jelly Castle, enumerated with uppercase English letters from A to Z. Because of security measures, each guest is known to be assigned an entrance he should enter the castle through. The door of each entrance is opened right before the first guest's arrival and closed right after the arrival of the last guest that should enter the castle through this entrance. No two guests can enter the castle simultaneously.

For an entrance to be protected from possible intrusion, a candy guard should be assigned to it. There are ksuch guards in the castle, so if there are more than k opened doors, one of them is going to be left unguarded! Notice that a guard can't leave his post until the door he is assigned to is closed.

Slastyona had a suspicion that there could be uninvited guests at the evening. She knows the order in which the invited guests entered the castle, and wants you to help her check whether there was a moment when more than k doors were opened.

Input

Two integers are given in the first string: the number of guests n and the number of guards k (1 ≤ n ≤ 1061 ≤ k ≤ 26).

In the second string, n uppercase English letters s1s2... sn are given, where si is the entrance used by the i-th guest.

Output

Output «YES» if at least one door was unguarded during some time, and «NO» otherwise.

You can output each letter in arbitrary case (upper or lower).

Examples
input
5 1
AABBB
output
NO
input
5 1
ABABB
output
YES

题目大意:

一个城堡将要迎接客人(客人用大写字母A-Z表示),客人有n个(字符串的顺序表示客人进入的顺序,同样的字母表示客人从同样的门进入),侍卫有k个。这个城堡有多个门,当每一个门有客人进入时必须有侍卫看守(防止其他人员进入),每一个侍卫的看守时间为:该门 第一个客人进入前 到 该门最后一个客人进入后 。如果该门没有客人要进入的话,那么当其它门需要侍卫时,该侍卫可以去其它门看守。题目问:如果至少一个门 在 有客人进入时 ,却没有侍卫看守 就输出“YES”,否则输出“NO”。

解题思路:

这道题想了一天半才做出来,感觉我的方法比较好。分享给大家:
用sum存储需要侍卫的数量(当然,sum初值是0),用一个map容器(map<char,int>M1)存储每个门客人的总数量,然后从字符串c[i]的开头开始遍历(这里还需要一个map容器,map<char,int>M2),如果该客人是  要从该门通过  的第一位客人(即M2[c[i]]==1),那么需要侍卫的数量sum++,但同时如果该客人是  要从该门通过  的最后一位客人(即M1[c[i]]==M2[c[i]]),那么说明该门的侍卫可以解放了(如果其它门需要,可以去其它门看守了),sum- -。然后判断sum是否大于k,如果大于,说明侍卫数量不够,输出YES,否则输出NO。看不懂的话,建议读者看一下代码,看懂后自己编写代码。希望能够帮助读者。

代码:


#include<iostream>
#include<map>
using namespace std;
int main()
{
    int n,k;
    char c[1000005];
    while(cin>>n>>k)
    {
        map<char,int>M1;   //在遍历的时候用到,其实就相当于中间变量,这里不懂的话继续往下看,看完就懂了
        map<char,int>M2;   //存储每个门 需要通过客人 的总数量
        int sum=0;  //存储当前需要的 侍卫 数量
        for(int i=0;i<=n-1;i++)
        {
            cin>>c[i];
            M2[c[i]]++;   //对应门(字母)的客人数量++
        }
        for(int i=0;i<=n-1;i++)
        {
            M1[c[i]]++;   //对应门(字母)的客人数量++
            if(M1[c[i]]==1)   //说明该客人 是 要通过该门的 第一位客人
            {
                sum++;   //既然是第一位客人,那么肯定要有侍卫去看守,sum++
                if(sum>k)   //如果所需客人数量 > 现有侍卫 数量,那么就没必要循环了(侍卫数量不够)
                    break;
            }
            if(M1[c[i]]==M2[c[i]])   //如果 该客人 是 通过该门的最后一位客人 ,那么这个侍卫就解放了,所需侍卫数量sum--
                sum--;
        }
        if(sum>k)   //说明侍卫数量不够
            cout<<"YES"<<endl;
        else   //说明侍卫数量够
            cout<<"NO"<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值