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 ...
随机推荐
- java高精度类尝试
java高精度尝试, poj2109,比较坑的题目 import java.io.*; import java.util.*; import java.math.*; public class Mai ...
- Charles 抓包发现自动跳转为https 问题梳理
今天遇到个有点意思的问题.特此记录. 业务场景: 做了一个页面,但是对外是挂载在京东主站上.如:www.jd.com/yifu/123456.html. 现场情况: 在本地/测试环境/预发环境中,每次 ...
- 【考试记录】4.8 Table ( 数论数学 --组合数 & 杨辉三角)
陆陆续续的开始考很多的试,也会更新这些题目记录下来,免得做完了之后毫无印象,就这么水过去了(以前的考试都是如此,哎……) Table (T1) : 样例: 出于对数学题本能的恐惧考场上放弃了此题专攻T ...
- [洛谷P2584][ZJOI2006]GameZ游戏排名系统
题目大意:同[洛谷P4291][HAOI2008]排名系统(双倍经验) 题解:略 卡点:无 C++ Code: #include <cstdio> #include <map> ...
- BZOJ2120 数颜色 【带修改莫队】
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MB Submit: 6579 Solved: 2625 [Submit][Status][Discus ...
- C&C++——extern
1.C 调用C++的函数或变量 C 调用C++的函数或变量,在C++的头文件声明为extern "C" ,C调用的时候只使用extern 声明. 可见,extern "C ...
- BZOJ_day9
哇,一道巨大的水题害得我wa了无数次... 总结一下教训 大家一定记住(给我自己看的) 位运算 一定要加()!!! 重要的事情说三遍 位运算 一定要加()!!! 位运算 一定要加()!!! 位运算 ...
- JavaScript渐变效果的实现
鼠标移上去透明度渐渐增加,鼠标移出,透明度渐渐减小. 关键代码: view source print? 1 var speed = 0; 2 if(target>obj.alpha){ 3 ...
- HDU 多校对抗赛 J Time Zone
Time Zone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- sls语法:创建file,创建文件夹
http://blog.kukafei520.net/html/2014/942.html /tmp/aaa.txt: file.managed /tmp/salt_test: file.direct ...