思路:
就是模拟一下吧。拿个优先队列存一下符合的颜色,每次对于每个是A的返回最小个数维护,就好了。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
struct asd{
int w;
int id;
bool operator < (const asd& t)const{
return w > t.w;
}
};
priority_queue<asd>q;
int ti[N];
bool vis[N];
int n, a, p[N];
int num[N];
int main(){
int x;
asd now;
scanf("%d%d",&n,&a);
memset(vis, 0, sizeof(vis));
memset(ti, 0, sizeof(ti));
for(int i=1;i<=n;i++){
scanf("%d", &p[i]);
vis[p[i]] = 1;
}
for(int i=1;i<=1000000;i++){
if(vis[i] && i != a){
now.id = i;
now.w = 0;
ti[now.id] = 1;
q.push(now);
}
}
memset(vis, 0, sizeof(vis));
memset(num, 0, sizeof(num));
for(int i=1;i<=n;i++){
x = p[i];
if(vis[x]) continue;
num[x]++;
if(x == a){
while(!q.empty()){
if(ti[q.top().id] > 1){
ti[q.top().id]--;
q.pop();
}
else{
if(q.top().w < num[a]){
vis[q.top().id] = true;
q.pop();
}
else break;
}
}
if(q.empty()){
puts("-1");
return 0;
}
}
else{
ti[x]++;
now.id = x;
now.w = num[x];
q.push(now);
}
}
printf("%d\n",q.top().id);
return 0;
}