1019A - Elections(枚举+模拟)

https://codeforces.com/problemset/problem/1019/A


思路:比较直接,枚举1号最后获胜的票数是多少,n^2允许。如果当前枚举过程中获得票数比预计的多了,就continue。不然就从小到大从没用的里面补

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=3e3+1000;
typedef long long LL;
typedef pair<pair<LL,LL>,LL > P;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
vector<LL>v[maxn];
map<pair<LL,LL>,bool>vis;
vector< pair<pair<LL,LL>,LL > >p;
bool cmp(P A,P B){
    return A.second<B.second;
}
int main(void)
{
  	cin.tie(0);std::ios::sync_with_stdio(false);
    LL n,m;cin>>n>>m;
    for(LL i=1;i<=n;i++){
        LL x,cost;cin>>x>>cost;
        v[x].push_back(cost);
    }
    for(LL i=1;i<=m;i++){
        sort(v[i].begin(),v[i].end());
    }
    for(LL i=2;i<=m;i++){
        for(LL j=0;j<v[i].size();j++){
            p.push_back({ {i,j},v[i][j]});
        }
    }
    sort(p.begin(),p.end(),cmp);
    LL ans=1e17;
    ///枚举1号获胜时候的票数
    for(LL j=v[1].size();j<=n;j++){
        vis.clear();
        LL have=v[1].size();LL res=0;
        for(LL i=2;i<=m;i++){
            if(v[i].size()>=j){
                LL cnt=v[i].size()-j+1;
                for(LL k=0;k<v[i].size();k++){
                    if(cnt<=0) break;
                    vis[{i,k}]=1;
                    res+=v[i][k];
                    have++;
                    cnt--;
                }
            }
        }
        if(have>j) continue;
        else{
            LL cnt=j-have;
            for(auto k:p){
                if(cnt<=0) break;
                if(vis[{k.first.first,k.first.second}]) continue;
                cnt--;
                res+=k.second;
            }
            ans=min(ans,res);
        }
    }
    cout<<ans<<"\n";
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/115294916
今日推荐