码迷,mamicode.com
首页 > 其他好文 > 详细

前缀和瞎搞下

时间:2018-09-18 19:16:28      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:phi   src   vector   set   print   input   清空   dex   输出   

题目奉上:

 

技术分享图片

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
Copy
5 1
AABBB
output
Copy
NO
input
Copy
5 1
ABABB
output
Copy
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.

这题在codeforses上无意间刷到的本以为可以开心的做水题没想道活生生被卡了好几个小时。

题目大意是:小明开糖果店,共有26个门从‘ A ’ 到 ‘ Z ’ ,之后来了n个客人,客人手中有票(标记从哪个门进入)而且小明知道客人进门的顺序。之后呢,小明共有k个守卫,每个守卫会把守一个大门,然后检票,直到走这个门的人没了,如果这样的话守卫会离开这个门去别的门把守(如果之后还有人会进入这个门的话守卫就不能离开)。问k个守卫是否足够把守,如果够输出 “NO ”,反之输出 ‘ YES ’ 

这个。。。。。我这语文二级半的水平也就能表达成这样了(虽然英语也差不多,咳咳。。。),如果题目没懂我来讲几个样例吧。

5 1

AAABB

答案是NO 因为呢第一个守卫大开A门放三个A进去,然后他知道后面没有A了(题目就说这个守卫眼神贼好反正就是知道这个门之后没人进了)然后他就把A门关上了去了B门知道人走光,所以一个守卫够用输出NO!!!!!!(我刚开始一直输出YES)。

5 1

ABABB

答案是YES 因为呢第一个守卫开A门放第一个人进去,然后第二个人是B,因为后面还有人要走A所以守卫不能离开还要在A哪里呆着,这时就需要第二个守卫去把守B门所以一个守卫是不够的就要有两个守卫所以答案是YES!!!!

希望上面已经把题目解释清楚了(如果不懂就谷歌一下下吧,原谅我这个语文,英语不好的人)接下来我先说下我在自己的思路

我刚开始想看所有带有相同字母的人是否连着,如果连着就不需要加一个守卫否则的话就要多来一个守卫看这个门。。。。。本以为无解的思路被无情的WA掉了样例也很不给面子贼简单 AABAABCCBC 这个按照我的结果来说是要有三个守卫的,结果告诉我俩就够用了因为第一个守卫看完A居然就走去看C门了。。。。然后我就懵逼了。

之后经过数小时的煎熬外加大佬指点之下我才弄明白只是一道前缀和的题。。。。。原谅我当时真的是没看出来这和前缀和有个毛关系。

这给就别讲前缀和为啥对吧,反正看的人八成是看不懂,我在代码中简单解释一下吧。

代码奉上:

技术分享图片
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<algorithm>
 5 #include<iostream>
 6 #include<math.h>
 7 #include<vector>
 8 #include<queue>
 9 using namespace std;
10 char x[1000181]; 
11 int y[30][3];//二维第一个表示当前这门进没进人,第二个表示第一个进去的序号,第三个表示最后一个进去的序号 
12 int z[1000181];//用来存当前这么多人需要多少守卫 
13 int main()
14 {
15     int m,n,sum;
16     while(~scanf("%d %d",&m,&n)){
17         memset(z,0,sizeof(z));//数组清空 
18         memset(y,0,sizeof(y));//数组清空 
19         scanf("%s",x);
20         for(int i=0;i<m;i++){
21             if(y[x[i]-‘A‘][0]==0){
22                 y[x[i]-‘A‘][0]=1;//表示进来过人了 
23                 y[x[i]-‘A‘][1]=i;//一定是刚进来的 
24                 y[x[i]-‘A‘][2]=i;//如果后面还有人就更新没有就和开始一样 
25             }
26             else y[x[i]-‘A‘][2]=i;//更新最后进来的 
27         }
28         for(int i=0;i<26;i++){
29             if(y[i][0]){
30                 z[y[i][1]]++;//在这进来人了需要加一个守卫 
31                 z[y[i][2]+1]--;//这个房间人都进去了守卫可以走了,必须要加一代表要有守卫看门不然的话就代表没有守卫看门了就会少守卫 
32             }
33         }
34         int sum=0;//当前需要多少守卫 
35         int flag=0;
36         for(int i=0;i<m;i++){
37             sum+=z[i];
38             //printf("sum=%d\n",sum);
39             if(sum>n) flag=1;//如果需要的守卫比有的守卫多记录 
40         }
41         //printf("%d\n",flag);
42         if(flag==0) printf("NO\n");
43         else printf("YES\n");
44     }
45     return 0;
46 }

前缀和瞎搞下

标签:phi   src   vector   set   print   input   清空   dex   输出   

原文地址:https://www.cnblogs.com/fzw1523/p/9670369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!