codeforces 818D Multicolored Cars

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.

Input

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

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
input
4 1
2 1 4 2
output
2
input
5 2
2 2 4 5 3
output
-1
input
3 10
1 2 3
output
4
Note

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.

题目大意:

一共n辆车,然后爱丽丝选择了一个编号m,给出一个长度为n的序列,让你在从中看是否能够找出一种编号k,使得无论在任何时刻,当前k的数量都比m的数量大,有的话随机输出一个正确的就OK,否则输出-1


题目思路:我们用一个桶装,但不是每个都装,只有当前k的数量大于m的数量的时候,我们才把k装进桶里,这样最后我们从头开始找的时候只要找到某个桶里的数量大于m桶里的数量就可以了,如果找不到输出-1


AC代码

#include<bits/stdc++.h>
using namespace std;
int a[1000001];
int main(){
    int n,k;
    scanf("%d%d", &n, &k);
    for(int i = 0; i < n; i++){
        int x;
        scanf("%d", &x);
        if(x == k){
            a[k]++;
        }
        else{
            if(a[x] >= a[k])
                a[x]++;
        }
    }
    for(int i = 1; i <= 1e6; i++){
        if(a[i] >= a[k] && i != k){
            printf("%d\n",i);
            return 0;
        }
    }
    printf("-1\n");
}


内容概要:该题库专为研究生入学考试计算机组成原理科目设计,涵盖名校考研真题、经典教材课后习题、章节题库和模拟试题四大核心模块。名校考研真题精选多所知名高校的计算机组成原理科目及计算机联考真题,并提供详尽解析,帮助考生把握考研命题趋势与难度。经典教材课后习题包括白中英《计算机组成原理》(第5版)和唐朔飞《计算机组成原理》(第2版)的全部课后习题解答,这两部教材被众多名校列为考研指定参考书目。章节题库精选代表性考题,注重基础知识与重难点内容,帮助考生全面掌握考试大纲要求的知识点。模拟试题依据历年考研真题命题规律和热门考点,精心编制两套全真模拟试题,并附标准答案,帮助考生检验学习成果,评估应试能力。 适用人群:计划参加研究生入学考试并报考计算机组成原理科目的考生,尤其是需要系统复习和强化训练的学生。 使用场景及目标:①通过研读名校考研真题,考生可以准确把握考研命题趋势与难度,有效评估复习成效;②通过经典教材课后习题的练习,考生可以巩固基础知识,掌握解题技巧;③通过章节题库的系统练习,考生可以全面掌握考试大纲要求的各个知识点,为备考打下坚实基础;④通过模拟试题的测试,考生可以检验学习成果,评估应试能力,为正式考试做好充分准备。 其他说明:该题库不仅提供详细的题目解析,还涵盖了计算机组成原理的各个方面,包括计算机系统概述、数据表示与运算、存储器分层、指令系统、中央处理器、总线系统和输入输出系统等。考生在使用过程中应结合理论学习与实践操作,注重理解与应用,以提高应试能力和专业知识水平。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值