码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 1019A Election

时间:2018-10-11 01:41:15      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:题意   class   color   empty   clear   枚举   pre   vector   long   

题意:每个人有信息(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\n");
            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

标签:题意   class   color   empty   clear   枚举   pre   vector   long   

原文地址:https://www.cnblogs.com/akira123/p/9769955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!