Educational Codeforces Round 24 D
Alice and Bob got very bored during a long car trip so they decided to play a game. From the window they can see cars of different colors running past them. Cars are going one after another.
The game rules are like this. Firstly Alice chooses some color A, then Bob chooses some color B (A?≠?B). After each car they update the number of cars of their chosen color that have run past them. Let‘s define this numbers after i
- If cntA(i)?>?cntB(i) for every i then the winner is Alice.
- If cntB(i)?≥?cntA(i) for every i then the winner is Bob.
- Otherwise it‘s a draw.
Bob knows all the colors of cars that they will encounter and order of their appearance. Alice have already chosen her color A and Bob now wants to choose such color B
If there are multiple solutions, print any of them. If there is no such color then print -1.
InputThe first line contains two integer numbers n and A (1?≤?n?≤?105,?1?≤?A?≤?106) – number of cars and the color chosen by Alice.
The second line contains n
Output such color B (1?≤?B?≤?106) that if Bob chooses it then he will win the game. If there are multiple solutions, print any of them. If there is no such color then print -1.
It is guaranteed that if there exists any solution then there exists solution with (1?≤?B?≤?106).
Examples input4 1output
2 1 4 2
2input
5 2output
2 2 4 5 3
-1input
3 10output
1 2 3
4Note
Let‘s consider availability of colors in the first example:
- cnt2(i)?≥?cnt1(i) for every i, and color 2 can be the answer.
- cnt4(2)?<?cnt1(2), so color 4 isn‘t the winning one for Bob.
- All the other colors also have cntj(2)?<?cnt1(2), thus they are not available.
In the third example every color is acceptable except for 10.
題意:兩個人玩遊戲,額,兩個人分別選一個數字,現在我們讓B贏,規則是
當前位置上,所選的數字比另外一個選的數字出現次數多或者一樣就算這個位置勝利了,從頭到尾一直是B勝利就行
比如 2 1 4 2 A選了1 B選2
2在第一位出現一次,1沒有出現,第一位勝利
2在第二位沒有出現,1出現一次,一共是1:1,也是勝利
第三位兩個數字都沒有,1:1,勝利
第四位2出現,最後是2:1 勝利
解法:
1 必輸的情況,A把第一個數字選了
2 必勝的情況,A沒有選C數組任何一個,那麽我們隨便選一個就行
3 如果某一個數字曾經輸於A,則不選
比如 A選2
3 2 2 2 3 那麽3輸了,不能選3
4 B不能選和A相同的數字,且出現次數比A選的數字要多。經過第3點的過濾,出現符合第4點的數字立馬輸出就行
1 #include<bits/stdc++.h> 2 using namespace std; 3 const long long N = 1e6+1; 4 vector<long long>v[123456]; 5 set<int>P; 6 long long A[N]; 7 map<int,int>Q,Mp; 8 int main() 9 { 10 int num,a; 11 int flag=0; 12 scanf("%d%d",&num,&a); 13 14 for(int i=1;i<=num;i++){ 15 scanf("%d",&A[i]); 16 17 if(A[i]==a){ 18 flag=1; 19 } 20 if(Q[A[i]]<Q[a]){ 21 Mp[A[i]]=-1; 22 } 23 Q[A[i]]++; 24 } 25 if(A[1]==a){ 26 printf("-1"); 27 return 0; 28 } 29 if(flag==1){ 30 for(int i=1;i<N;i++){ 31 if(Q[A[i]]>=Q[a]&&Mp[A[i]]!=-1&&A[i]!=a){ 32 printf("%d",A[i]); 33 return 0; 34 } 35 } 36 printf("-1"); 37 return 0; 38 39 }else{ 40 printf("%d",A[1]); 41 } 42 return 0; 43 }
Educational Codeforces Round 24 D