codeforces 1019A Election

Posted akira123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces 1019A Election相关的知识,希望对你有一定的参考价值。

题意:每个人有信息(p,c): 给序号为p的party投票,收买此人的价格为c

           目的是要求序号为1的party投票数最多,问最少需要花费多少收买别人?

#include <bits/stdc++.h>

using namespace std;
const int maxn = 3050;
int N,M;
vector<long long> G[maxn];
long long ans = 1e13;
int main() {
    scanf("%d%d",&N,&M);
    int p;
    long long c;
    for (int i = 0;i < N;i++) {
        cin>>p>>c;
        G[p].push_back(c);
    }
    int maxVotes = 0;
    for (int i = 1;i <= M;i++) {
        if (!G[i].empty()) {
            sort(G[i].begin(),G[i].end());
            if (G[i].size() > maxVotes) {
                maxVotes = G[i].size();
            }    
        }
    } 
    //枚举 party 获胜所得到的票数
    vector<long long> left; 
    int votes = 0;
    long long tmp;
    for (int i = 1;i <= N;i++) {
        //先计算降低其他party票数必须付的最少的钱 
        //i 从 G[1].size()开始时此程序有bug,因为若G[1].size() = 0,由于G[j].size() >= i
        //进入for (k = 0;k < G[j].size() - i + 1;k++),若此时G[j].size == 0,则还会访问G[j][0] 
               //或加上判断 if (G[j].size() > 0)
        left.clear();
        votes = 0;
        tmp = 0;
        for (int j = 2;j <= M;j++) {
            int k = 0;
            
            if (G[j].size() >= i) {
                votes += (G[j].size() - i + 1);
                for (k = 0;k < G[j].size() - i + 1;k++) {
                    tmp += G[j][k];
                }
                for (;k < G[j].size();k++) {
                    left.push_back(G[j][k]);
                }
            }else {
                for (k = 0;k < G[j].size();k++) {
                    left.push_back(G[j][k]);
                }
            }
        }
        if (G[1].size() + votes < i) {
//            printf("not enough
");
            sort(left.begin(),left.end());
            for (int k = 0;k < i - G[1].size() - votes && k < left.size();k++) {
                tmp += left[k];
            }
        }
//        cout<<"votes: "<<i<<" tmp: "<<tmp<<endl;
        if (tmp < ans) {
            ans = tmp;
        }
    } 
    cout<<ans<<endl;
    return 0;
}          

 

以上是关于codeforces 1019A Election的主要内容,如果未能解决你的问题,请参考以下文章

瞎搞题gym226123 L. For the Honest Election

TOJ-1322 Election

election.go

AtCoDeer and Election Report

[LeetCode] 911. Online Election 在线选举

AtCoDeer and Election Report