Codeforces Round #503 (by SIS, Div. 2) C. Elections

Keywords: less

Topic link: http://codeforces.com/contest/1020/problem/C

 

Description of the title:

There are n voters, m political parties (n, m <= 3000), and the first voter has two pi, ci (pi <= m, ci <= 1e9), which means that the voter votes for the party pi, but if you give him ci gold coin, he will change his voting target to party 1 (party number 1 to m).

The requirement now allows the party to have the highest number of votes (no juxtaposition) and the minimum number of gold coins to be spent.

Topic analysis:

At first, this question wanted to be answered by the gold coin that happened to get the highest number of votes, but the dichotomy was wrong.

For example:

5. (5 voters, 5 political parties)
2 100 (Voter No. 1 votes for Party No. 2, which costs $100 to vote for Party No. 1)
3 200
4 300
5 800
5 900

It happens that the highest number of votes is 2 (buy No. 4 and No. 1), and the cost is 900.

But if you buy No. 1, No. 2 and No. 3, you can also get the highest number of votes at a cost of 600, which is obviously the right answer.

Positive solution:

By enumerating the gold coins needed to obtain (1 to n) votes for Party 1, the minimum cost can be obtained for the case with the highest number of votes.

Code:

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f
#define ll long long
using namespace std;
int n,m,x,y;
vector<int>v[3005];
ll cal(int k){
    vector<int>bk;
    ll res=0;
    ll cnt=v[1].size();//Party 1: Number of votes available
    for(int i=2;i<=m;i++){
        int num=v[i].size();
        for(int j=0;j<num;j++){
            if(num-j>=k){       //num-j is the number of votes available for party i
                res+=v[i][j];   //If it is larger than the number of votes to be obtained k
                cnt++;          //Need to buy it as the number of votes for party 1
            }else bk.push_back(v[i][j]);    //If it's not bigger than that, throw it in the bucket first.
        }
    }
    if(cnt<k){  //If Party 1 does not have enough votes available for our target k-votes
        sort(bk.begin(),bk.end());//You need to pick out the cheapest one among the voters who are throwing barrels.
        for(int i=0;i<bk.size()&&cnt<k;i++){
            res+=bk[i];
            cnt++;
        }
    }
    return res; //If the enumerated k is less than or equal to n, the number of votes k must be reached. There is no solution.
}
int main(){
    while(~scanf("%d %d",&n,&m)){
        for(int i=1;i<=m;i++)v[i].clear();
        for(int i=1;i<=n;i++){
            scanf("%d %d",&x,&y);
            v[x].push_back(y);
        }
        for(int i=1;i<=m;i++){//Cheaply rank the voters of all political parties
            sort(v[i].begin(),v[i].end());
        }
        ll ans=INF;
        for(int k=1;k<=n;k++){   //Enumeration of target votes for Party 1
            ans=min(ans,cal(k));
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

Posted by altergothen on Sun, 03 Feb 2019 22:57:16 -0800