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-th car cntA(i) and cntB(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 that he will win the game (draw is not a win). Help him find this color.
If there are multiple solutions, print any of them. If there is no such color then print -1.
The 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 integer numbers c1, c2, ..., cn (1 ≤ ci ≤ 106) — colors of the cars that Alice and Bob will encounter in the order of their appearance.
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).
4 1
2 1 4 2
2
5 2
2 2 4 5 3
-1
3 10
1 2 3
4
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 }