CF818D.Multicolored Cars
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 cnt__A(i) and cnt__B(i).
- If cnt__A(i) > cnt__B(i) for every i then the winner is Alice.
- If cnt__B(i) ≥ cnt__A(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.
爱丽丝和鲍勃在漫长的汽车旅行中感到非常无聊,于是决定玩一个游戏。透过车窗,他们可以看到一辆接一辆驶过的、颜色各异的汽车。
游戏规则如下:首先,爱丽丝选择某种颜色 A,然后鲍勃选择另一种颜色 B(满足 A=B)。每驶过一辆汽车后,他们分别更新自己所选颜色的汽车数量。记第 i 辆汽车驶过之后,颜色 A 和颜色 B 的累计数量分别为 cntA(i) 和 cntB(i)。
- 若对所有 i 均有 cntA(i)>cntB(i),则爱丽丝获胜;
- 若对所有 i 均有 cntB(i)≥cntA(i),则鲍勃获胜;
- 否则为平局。
鲍勃已知他们将遇到的所有汽车的颜色及其出现顺序。爱丽丝已选定她的颜色 A,而鲍勃现在希望选择一个颜色 B,使得他能赢得游戏(平局不算获胜)。请帮助他找出这样的颜色。
若存在多个可行解,输出任意一个即可;若不存在这样的颜色,则输出 −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 _c_1, _c_2, ..., c__n (1 ≤ c__i ≤ 106) — colors of the cars that Alice and Bob will encounter in the order of their appearance.
第一行包含两个整数 n 和 A(1 ≤ n ≤ 105,1 ≤ A ≤ 106)——分别为汽车的数量以及爱丽丝选择的颜色。
第二行包含 n 个整数 c1,c2,...,cn(1 ≤ ci ≤ 106)——表示爱丽丝和鲍勃将按顺序遇到的汽车的颜色。
输出格式
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).
输出一个颜色 B(1 ≤ B ≤ 10⁶),使得若 Bob 选择该颜色,则他将赢得游戏。若存在多个解,输出任意一个即可。若不存在这样的颜色,则输出 -1。
题目保证:若存在解,则必存在满足 (1 ≤ B ≤ 10⁶) 的解。
输入输出样例
输入#1
4 1 2 1 4 2
输出#1
2
输入#2
5 2 2 2 4 5 3
输出#2
-1
输入#3
3 10 1 2 3
输出#3
4
说明/提示
Let's consider availability of colors in the first example:
- _cnt_2(i) ≥ _cnt_1(i) for every i, and color 2 can be the answer.
- _cnt_4(2) < _cnt_1(2), so color 4 isn't the winning one for Bob.
- All the other colors also have cnt__j(2) < _cnt_1(2), thus they are not available.
In the third example every color is acceptable except for 10.
我们来考虑第一个例子中各颜色的可用性:
- 对每个 i,都有 cnt2(i)≥cnt1(i),因此颜色 2 可以是答案。
- cnt4(2)<cnt1(2),所以颜色 4 不是 Bob 的获胜颜色。
- 其余所有颜色也都满足 cntj(2)<cnt1(2),因此它们均不可用。
在第三个例子中,除颜色 10 外,其余所有颜色均可接受。
输入解题思路,AI测评打分。不知道怎么写?