CF834B-The Festive Evening

来源:互联网 发布:淘宝网页怎么发链接 编辑:程序博客网 时间:2024/05/22 17:38

B. The Festive Evening

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 k such 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 ≤ 106, 1 ≤ 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
Note
In the first sample case, the door A is opened right before the first guest’s arrival and closed when the second guest enters the castle. The door B is opened right before the arrival of the third guest, and closed after the fifth one arrives. One guard can handle both doors, as the first one is closed before the second one is opened.

In the second sample case, the door B is opened before the second guest’s arrival, but the only guard can’t leave the door A unattended, as there is still one more guest that should enter the castle through this door.

题目大意:有n个客人,每个客人从A~Z的门进,有k个守卫,给出客人到来顺序,守卫必须在某种门等到最后一个客人,问是否存在门没有守卫的情况。
解题思路:抽象一下就是,求客人最多被多少个区间覆盖。

#include<iostream>#include<cstdio>#include<vector>#include<map>#include<set>#include<queue>#include<cmath>#include<string>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const int INF=0x3f3f3f3f;const int MAXN=1e6+5;const double eps=1e-10;int a[MAXN],n;int cnt[MAXN];//char s[MAXN];int main(){    int n,k;    string s1,s2;    while(scanf("%d%d",&n,&k)!=EOF)    {        memset(cnt,0,(n+3)*sizeof(int));        cin>>s1;        s2=s1;        reverse(s2.begin(),s2.end());        //cout<<s2<<endl;        for(int i=0;i<26;++i)        {            auto p=s1.find((char)('A'+i));            auto q=s2.find((char)('A'+i));            if(p!=string::npos)            {                q=n-1-q;                for(int j=p;j<=q;++j) ++cnt[j];            }        }        int ans=-INF;        for(int i=0;i<n;i++)            ans=max(cnt[i],ans);        if(ans>k) printf("YES\n");        else printf("NO\n");    }    return 0;}
原创粉丝点击