前綴和瞎搞下
題目奉上:
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.
InputTwo 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.
OutputOutput ?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 Copy5 1output Copy
AABBB
NOinput Copy
5 1output Copy
ABABB
YESNote
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 }
前綴和瞎搞下