The Festive Evening(思维题)
来源:互联网 发布:linux中连接本地 编辑:程序博客网 时间:2024/05/22 13:47
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.
这道题就是一个思维题,我们只需要记录每个门最开始和结束的客人的位置就行了,当每个门遇到第一位应该来的客人,守卫+1,遇到每个人最后一个客人守卫-1,记录最大需要多少个守卫,在和题目里的比较就好。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;char s[1000010];int book_head[200];int book_tail[200];int main(){ int n,m; scanf("%d%d",&n,&m); scanf("%s",s); int max_=0; int cnt=0; memset(book_head,-1,sizeof(book_head)); memset(book_tail,-1,sizeof(book_tail)); for(int i=0;i<n;i++) //记录每个门第一个客人的位置 { if(book_head[s[i]]==-1) { book_head[s[i]]=i; } } for(int i=n-1;i>=0;i--) //记录每个门最后一个客人的位置 { if(book_tail[s[i]]==-1) { book_tail[s[i]]=i; } } for(int i=0;i<n;i++) { if(i==book_head[s[i]]) { cnt++; //遇到第一位就+1 if(cnt>max_) { max_=cnt; //记录最大守卫值 } if(max_>m) { printf("YES\n"); return 0; } } if(i==book_tail[s[i]]) { cnt--; //遇到最后一位客人就-1 } } if(max_>m) { printf("YES\n"); } else { printf("NO\n"); }}
- The Festive Evening(思维题)
- The Festive Evening (思维题目)
- Codeforces834B The Festive Evening
- CF834B-The Festive Evening
- The Festive Evening map
- The Festive Evening
- The Festive Evening
- The Festive Evening (Codeforces
- cf The Festive Evening(水)
- 守卫问题The Festive Evening
- Codeforces Round #426 (Div. 2) B. The Festive Evening(思维)
- CodeFroces 834B. The Festive Evening(暴力)
- Codeforces 834B-The Festive Evening
- 【CodeForces】426Div2 B The Festive Evening
- Codeforces Round 834B-The Festive Evening
- Codeforces 834 B The Festive Evening
- CodeForces #426 div2 B The Festive Evening
- CodeForces 834B The Festive Evening
- mongoose中connect()、createConnection()和connection的区别和作用
- javascript学习笔记:BOM对象(存疑)
- ARM-Linux移植之(一)——内核启动流程分析
- 一些精致的web UI框架及常用前端UI框架
- 数字变成大写金额
- The Festive Evening(思维题)
- C++ auto
- 通过KBUILD_MODNAME让dev_err等函数打印模块的name
- 万里长征第一步----学会做科研
- Android数据库(SQLite)框架(8)—— 总结
- 关于hashCode()与equals()的总结,所要知道的
- MAC上好用的 PDF编辑软件 PDFpenPro
- MySQL中GROUP_CONCAT中的排序和替换
- 纯JS省市区联动