Codeforces 834B-The Festive Evening

来源:互联网 发布:杀马特火星文 知乎 编辑:程序博客网 时间:2024/06/05 10:10

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 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 ≤ 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 1AABBB
output
NO
input
5 1ABABB
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.


题意:一个城堡有26扇门,但每段时间只能开k扇门,每扇门从最早进入的人打开,最晚进入的人关闭,有n人轮流进入城堡,给出他们是从哪扇门进入的,问是否有时间有大于k扇门打开

解题思路:找出每扇门的打开时间和关闭时间,然后在这两个时间标记一下,最后跑一次就可以知道每个时间段打开的门的个数



#include <iostream>  #include <cstdio>  #include <cstring>  #include <string>  #include <algorithm>  #include <map>  #include <set>  #include <stack>  #include <queue>  #include <vector>  #include <bitset>  #include <functional>using namespace std;#define LL long long  const int INF = 0x3f3f3f3f;char ch[1000009];int k,n;int sum[1000009], a[30];int main(){while (~scanf("%d%d", &n, &k)){scanf("%s", ch+1);memset(sum, 0, sizeof sum);memset(a, -1, sizeof a);for (int i = 1; i <= n; i++){int p = ch[i] - 'A';if (a[p] == -1) a[p] = i;}for (int i = 0; i < 26; i++)if (a[i] != -1) sum[a[i]]++;memset(a, -1, sizeof a);for (int i = n; i >0; i--){int p = ch[i] - 'A';if (a[p] == -1) a[p] = i;}for (int i = 0; i < 26; i++)if (a[i] != -1) sum[a[i] + 1]--;int flag = 1;for (int i = 1; i <= n; i++){sum[i] += sum[i - 1];if (sum[i] > k) flag = 0;}if (flag) printf("NO\n");else printf("YES\n");}return 0;}

原创粉丝点击