Codeforces818D【优先队列】

本文介绍了一种使用优先队列的数据结构来解决颜色选择问题的方法。通过维护一个优先队列来跟踪每种颜色出现的次数,并根据特定条件选择颜色。文章提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:
就是模拟一下吧。拿个优先队列存一下符合的颜色,每次对于每个是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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值