众数

D

题意:

给定一个数组,每次挑选其中两个不同的数,使其变为a+1,b-1.求操作后的众数

思路:

属于是stl的千层妙用了

开一个set,左键值设为数的数量,右键值设为数。这样数量多的排在最后面,数量相当时,大的排在后面。此时我们可以用rbegin()方法获得集合中最后一个元素

然后就是模拟

需要注意每次要改变桶的值,而非单纯将-1后的值塞进去。因为两个操作有可能同时作用在一种数字上,此时会出现冲突

int n;
int tot[maxn];
set<pii>st;
void add(int x){
    if(tot[x]){
        st.erase({tot[x],x});
    }
    tot[x]++;
    st.insert({tot[x],x});
}
void del(int x){
    st.erase({tot[x],x});
    tot[x]--;
    if(tot[x]){
        st.insert({tot[x],x});
    }
}
void solve(){
    cin>>n;
    vector<int>a(n+1);
    rep(i,1,n){
        cin>>a[i];
        tot[a[i]]++;
    }
    for(int i=1;i<=1e6;i++){
        if(tot[i]){
            st.insert({tot[i],i});
        }
    }
    set<int>vis;
    rep(i,1,n){
        add(a[i]+1);del(a[i]);
        rep(j,1,n){
            if(i==j)continue;
            add(a[j]-1);del(a[j]);
            vis.insert((*st.rbegin()).se);
            del(a[j]-1);add(a[j]);
        }
        del(a[i]+1);add(a[i]);
    }
    for(auto ans:vis){
        cout<<ans<<' ';
    }cout<<endl;
}
posted @ 2025-05-31 08:32  Marinaco  阅读(22)  评论(0)    收藏  举报
//雪花飘落效果