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
*/
CF 834B The Festive Evening【差分+字符串处理】的更多相关文章
- [CF 295A]Grag and Array[差分数列]
题意: 有数列a[ ]; 操作op[ ] = { l, r, d }; 询问q[ ] = { x, y }; 操作表示对a的[ l, r ] 区间上每个数增加d; 询问表示执行[ x, y ]之间的o ...
- [ 9.29 ]CF每日一题系列—— 765B字符串规律
Description: 遇到了ogo可以变成***如果ogo后面有go统统忽略,输出结果 Solution: 哎如果我一开始对题意的解读如上的话,就不会被整的那么麻烦了 Code: #include ...
- 【Codeforces Round #426 (Div. 2) B】The Festive Evening
[Link]:http://codeforces.com/contest/834/problem/B [Description] [Solution] 模拟水题; 注意一个字母单个出现的时候,结束和开 ...
- Codeforces Round #426 (Div. 2) B题【差分数组搞一搞】
B. The Festive Evening It's the end of July – the time when a festive evening is held at Jelly Castl ...
- Lyndon 相关的炫酷字符串科技
浅谈从 Lyndon Words 到 Three Squares Lemma By zghtyarecrenj 本文包括:Lyndon Words & Significant Suffixes ...
- Codeforces Round #426 (Div. 2)A B C题+赛后小结
最近比赛有点多,可是好像每场比赛都是被虐,单纯磨砺心态的作用.最近讲的内容也有点多,即便是点到为止很浅显的版块,刷了专题之后的状态还是~"咦,能做,可是并没有把握能A啊".每场网络 ...
- Codeforces Round #426 (Div. 2)A题&&B题&&C题
A. The Useless Toy:http://codeforces.com/contest/834/problem/A 题目意思:给你两个字符,还有一个n,问你旋转n次以后从字符a变成b,是顺时 ...
- javascript运算符与表达式
表达式 表达式是关键字.运算符.变量以及文字的组合,用来生成字符串.数字或对象.一个表达式可以完成计算.处理字符.调用函数.或者验证数据等操作. 表达式的值是表达式运算的结果,常量表达式的值就是常量本 ...
- http://codeforces.com/contest/834
A. The Useless Toy time limit per test 1 second memory limit per test 256 megabytes input standard i ...
随机推荐
- 【bzoj2326】[HNOI2011]数学作业 矩阵乘法
题目描述 题解 矩阵乘法 考虑把相同位数的数放到一起处理: 设有$k$位的数为$[l,r]$,那么枚举从大到小的第$i$个数(即枚举$r-i+1$),考虑其对$Concatenate(l..r)$的贡 ...
- 【bzoj2152】聪聪可可 树的点分治
题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好了,可是他们已 ...
- Visio中设置自定义属性的值
ShapeSheet中User-Define Cells中Prompt格. 帮助解释为:为用户定义的单元格指定说明性提示或注释.应用程序自动将提示文本用引号 (" ") 引起来,以 ...
- [Leetcode] Merge k sorted lists 合并k个已排序的链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...
- 小K的农场
小K的农场 题目描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种形式描述: 农场a比农场b至少多种植了 ...
- python3初识selenium
第一步:安装与配置 1.电脑上需要有火狐浏览器(默认安装在C:\Program Files (x86)\Mozilla Firefox目录下). 2.使用pip install selenium安装好 ...
- bzoj 2566 calc 拉格朗日插值
calc Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 377 Solved: 226[Submit][Status][Discuss] Descr ...
- Java中文乱码问题(转)
解决JSP中文乱码问题 大家在JSP的开发过程中,经常出现中文乱码的问题,可能一至困扰着大家,现把JSP开发中遇到的中文乱码的问题及解决办法写出来供大家参考.首先了解一下Java中文问题的由来: Ja ...
- 【洛谷 P1631】 序列合并 (堆)
题目链接 直接暴力搞是\(n\)方的复杂度.\(n^2\)个数选\(n\)个最小的,容易想到堆. 我们堆里记录两个信息:到\(A\)数组哪个位置了,到\(B\)数组哪个位置了, 我直接把这两个信息存在 ...
- Codeforces 940F Machine Learning 带修改莫队
题目链接 题意 给定一个长度为\(n\)的数组\(a\),\(q\)个操作,操作分两种: 对于区间\([l,r]\),询问\(Mex\{c_0,c_1,c_2,⋯,c_{10^9}\}\),其中\(c ...