Codeforces Round 834B-The Festive Evening

2023-12-26 05:33

本文主要是介绍Codeforces Round 834B-The Festive Evening,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

一座城堡有26个门,分别用A~Z表示。有n位客人要进入这个城堡。客人进入城堡需要通过这些门。
每一道门,在第一位客人进来先打开,在通过这道门的最后一位客人进入以后关闭。
(一个潜在的坑= =)
城堡的主人为这些门配备了k位守卫。守卫不可以在某一道门没有关上的情况下离开这道门,以防止有多位客人同时通过一道门的情况。给出n、k以及客人们进入门的顺序。求有没有可能发生有超过k个门打开的情况。

我的做法是,记录下每一道门打开的时间和门关上的时间。
为了方便起见我用了map来实现。
mp1记录每道门打开的时间。mp2记录门关闭的时间。
时间用客人的进门的次序来表示。用一个变量m来记录,每一个时刻,有多少个门开着。
根据客人进门顺序从头到尾遍历,查询有没有哪个时刻,有超过k个门开着,如果有直接break,输出结果YES

不过有一个坑是在题目的描述上。
题目上说了,门是在客人到来之前就打开的
也就是说,并不是客人来了你才去开门,而是前一个客人进门的时候守卫就需要去开门的
最开始没有考虑这个点,在第四个test就WA了。
所以这个题目,在一开始,第一位客人要进入的那个门就是开着的。
所以m的初始值我设为1,当遍历到当前的客人时,查询下一个门此时是否应该开启。
如果不考虑这种情况的话,有可能有超过k个门开启的情况会被漏掉。
这是需要注意的点。

具体代码如下:

#include<string>
#include<cmath>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<algorithm>
#include<stdio.h>
#include<cstdio>
#define Max 100000using namespace std;
map<char,int> mp1,mp2;//开始、结束void f()
{char c='A';for(; c<='Z'; c++){mp1[c]=-1;mp2[c]=-1;}
}void clr()
//清空
{mp1.clear();mp2.clear();
}int main()
{char a,b,c;string s;bool flag;int T;int i,j,k,t;int m,n,l;bool flag1,flag2;while(cin>>n>>k){cin>>s;clr();f();flag=true;for(i=0; i<n; i++){if(mp1[s[i]]==-1)
//判断开始值是否已存在mp1[s[i]]=i;mp2[s[i]]=i;
//一直更新mp2[s[i]]值,这样最后保存下来的一定是最晚的那个客人的进门时间}for(j=0,m=1; j<n; j++){if(j+1==mp1[s[j+1]])
//考虑下一位客人进入的门是否需要打开m++;if(j==mp2[s[j]])
//当前客人进入的门是否需要开启m--;if(m>k){flag=false;break;}}if(flag)printf("NO\n");else printf("YES\n");}return 0;
}

这篇关于Codeforces Round 834B-The Festive Evening的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/538276

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

Codeforces#295(Div.2)A、B(模拟+BFS)

解题报告链接:点击打开链接 C. 题目链接:点击打开链接 解题思路: 对于给定的字符串,取出现次数最多的字母(可以同时有多个)。由这些字母组成长度为n的字符串,求有多少种组合。最后用数学知识即可。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <climits>

MemSQL Start[c]UP 2.0 - Round 1A(构造)

题目链接:http://codeforces.com/problemset/problem/452/A 解题思路: 打个表暴力查找匹配。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <complex>#include <cstdio>#include <strin

Codeforces Round #281 (Div. 2)A(构造+暴力模拟)

题目链接:http://codeforces.com/problemset/problem/493/A 解题思路: 暴力的判断,分三种情况去判断即可。注意如果之前已经被罚下场后,那么在后面的罚下情况不应该算在输出结果内。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <co

Codeforces Round #182 (Div. 2)A(水题)

题目链接:http://codeforces.com/contest/302/problem/A 解题思路: 只要通过重新排列使区间内和为0即是1,否则是0. 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <complex>#include <cstdio>#inc