CF 834B The Festive Evening【差分+字符串处理】

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
inputCopy
5 1
AABBB
outputCopy
NO
inputCopy
5 1
ABABB
outputCopy
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.
【题意】:输入一堆大写字母,每个字符从第一次出现到最后一次出现的这段时间内需要一个守卫,问你在给定k给守卫的条件下,总需求会不会超过k个守卫。
【分析】:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
#include<bits/stdc++.h>
using namespace std;
const int maxn=200005;
typedef long long ll;
ll p[maxn];
ll x[maxn];
ll n,q;
ll a[maxn];

char s[1000100];
map <char,int>mp;
int num[1000100];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    getchar();
    gets(s);
    for(int i = 0; s[i]; i++)
    {
        if(mp[s[i]] == 0) //当入口最后一个访客进入
        {
            num[i]++;
        }
        mp[s[i]]++;
    }
    for(int i = 0; s[i]; i++)
    {
        mp[s[i]]--;
        if(mp[s[i]] == 0)//当入口最后一个访客进入
        {
           num[i+1]--;
        }
    }
    for(int i = 1; s[i]; i++)
    {
        num[i] += num[i-1];
    }
    for(int i = 0; s[i]; i++)
    {
        if(num[i] > k)
        {
            printf("YES\n");
            return 0;
        }
    }
    printf("NO\n");
    return 0;
}
/*
输入一堆大写字母,每个字符从第一次出现到最后一次出现的这段时间内需要一个守卫,问你在给定k给守卫的条件下,总需求会不会超过k个守卫。
思路:比如说,A出现了多次,那么在A的第一次出现的位置i处num[i]++,最后一个A在j处num[j+1]--,其他字母类似,这样处理后,对num数组前n项和处理,每个位置就是那个时刻开门的个数了,然后一次遍历就行了。
因为已知访客的入口顺序, 先记录每个入口的访客数量
然后循环, 若该入口第一个访客进入, 守卫减1, 当入口最后一个访客进入, 守卫加1
其中注意守卫为负数时, 表示不能防止有人混入
抽象一下就是,求客人最多被多少个区间覆盖。
5 1
AABBB
01
24
NO

5 1
ABABB

YES
*/
posted @ 2018-07-23 16:10  Roni_i  阅读(196)  评论(0编辑  收藏  举报