Codeforces 834B-The Festive Evening
来源:互联网 发布:杀马特火星文 知乎 编辑:程序博客网 时间:2024/06/05 10:10
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.
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 «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).
5 1AABBB
NO
5 1ABABB
YES
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;}
- Codeforces 834B-The Festive Evening
- Codeforces Round 834B-The Festive Evening
- Codeforces 834 B The Festive Evening
- CodeForces 834B The Festive Evening
- CodeForces 834 B.The Festive Evening(水~)
- 【CodeForces】426Div2 B The Festive Evening
- CodeForces #426 div2 B The Festive Evening
- The Festive Evening (Codeforces
- CodeFroces 834B. The Festive Evening(暴力)
- CF 834B-The Festive Evening
- codefores 834B The Festive Evening
- CF 834B The Festive Evening
- B. The Festive Evening(Codeforces Round #426 (Div. 2) B)
- Codeforces Round #426 (Div. 2) B. The Festive Evening
- Codeforces Round #426 (Div. 2)-B. The Festive Evening
- Codeforces Round #426 (Div. 2) B The Festive Evening
- (模拟)Codeforces Round #426 B. The Festive Evening
- Codeforces Round #426 (Div. 2) B. The Festive Evening
- 结合FlexboxLayout实现标签选择功能
- ionic3(一)安装和配置
- php判断密码强度
- js 中处理金额数据 四舍五入数据(,处理和保留小数处理)
- svn插件集成myeclipse2014的两种方式
- Codeforces 834B-The Festive Evening
- java实现点击的图片验证码
- Raspberry Pi 3B I2C连接MPU-6050六轴感测器
- storyboard Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]
- flume高并发优化——(16)解决offsets变小问题
- webservice之cxf服务端及客户端实现(maven创建web项目)
- Linux的JCPU与PCPU区别
- #5. 【NOI2014】动物园
- spring ContentNegotiationManagerFactoryBean 内容协商