The Festive Evening

来源:互联网 发布:caffe windows github 编辑:程序博客网 时间:2024/05/22 15:10

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 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.

这个题我的思路一开始想错了,想把他们都化为一个线段的问题,然后利用自己的线段重叠的问题,来解决这个问题,可是我发现,自己的这种线段,只能说是最好情况下,不相邻的最多的个数,没有办法来实现这样的最大重叠的问题。我们可以把这种相邻的问题,放在一个一维数组中,然后记录起点和终点。就像是,放一个警卫在A门口,从i开始到j,所以我们记录这个起始和结束的时间,在i时间点,放下,在j+1,时间点,我们就把它拿走了,所有的字母都是采用这样的处理,然后直接统计pos[1]+pos[2]+…+pos[n],看看过程中,最多是多少个警卫,这就是最多的警卫的数量。

#include<cstdio>#include<iostream>#include<cstring>#include<math.h>#include<map>using namespace std;const int Max=1e6+10;map<char,int> mp;//直接将字母当做下标int pos[Max];//记录警卫的位置int main(){   memset(pos,0,sizeof(pos));   int n,k;   scanf("%d %d",&n,&k);   for(int i=1;i<=n;i++)   {       char temp;       scanf(" %c",&temp);       if(mp[temp]==0)       {           pos[i]++;//在i 位置放下一个警卫       }       mp[temp]=i;   }   for(auto it:mp)   {       pos[it.second+1]--;//他撤走之后的下一个位置就少一个人   }   int ans=1;   for(int i=2;i<=n;i++)   {       pos[i] += pos[i-1];       if(pos[i]>ans)        ans=pos[i];   }   if(k>=ans)    printf("NO\n");   else    printf("YES\n");   return 0;}