Codeforces1020C Elections
题目
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon.
Elections are coming. You know the number of voters and the number of parties — n and m respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give -th voter bytecoins you can ask him to vote for any other party you choose.
The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party.
输入
The first line of input contains two integers n and m (1<=n,m<=3000) — the number of voters and the number of parties respectively.
Each of the following lines contains two integers pi and ci (1<=pi<=m,1<=ci<=10^9) — the index of this voter’s preferred party and the number of bytecoins needed for him to reconsider his decision.
The United Party of Berland has the index 1.
输出
Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections.
样例
input
1 2
1 100
output
0
input
5 5
2 100
3 200
4 300
5 400
5 900
output
500
input
5 5
2 100
3 200
4 300
5 800
5 900
output
600
提示
In the first sample, The United Party wins the elections even without buying extra votes.
In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties 3, 4 and 5 get one vote and party number 2 gets no votes.
In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
思路
考虑贪心,金钱从小到大收买投票者,发现收买票数高的候选人的投票者可能得到更优解,进一步发现1号候选人得票数不是固定的,所以要枚举1号候选人所有可能的得票数,并在得票数不同情况中选择最小的花费,就是最优解。
对于每种可能的得票数,除1号候选人外的其他候选人必须小于此得票数,如果不小于,1号候选人就不可能超过其他所有候选者,也就不符题意。所以不小于此得票数的候选人的投票者会被收买直到小于此得票数。贪心考虑,同一个候选人从金钱少的投票者买起。
一旦此过程中1号候选人的票数大于此得票数的话,这种情况不存在,因为1号候选人到达此票数时还有其他候选人不小于此票数。
得票数最低只能是n/m,如果低于n/m,则其他候选人必须小于n/m,1号候选人的得票数必定大于n/m,从而这种情况不存在。
如果此过程结束后1号候选人还没有到达此票数,那么在剩下的投票者中从金钱少的买起,直到1号候选人达到此票数。这是此票数情况下的最优解。
因为有从金钱少的投票者买起的操作,所以对投票者的金钱从小到大排序。枚举所有投票者,如果该投票者投的候选人不小于此票数就收买,如果全都小于此票数就收买金钱少的投票者。
代码
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)
#define clr(arr,val) memset(arr,val,sizeof arr)
typedef long long ll;
const int maxn=3e3+10;
const ll inf=LONG_LONG_MAX;
using namespace std;
int n,m,cnt[maxn],tcnt[maxn];
//cnt表示候选人的得票数,从0开始
//tcnt表示对于每种可能的得票数候选人的得票数,从0开始
bool vis[maxn];//是否被收买
struct Voter
{
int party;
int money;
}voter[maxn];
bool cmp(Voter a,Voter b)
{
return a.money<b.money;
}
int main()
{
cin>>n>>m;
rep(i,0,n)
{
scanf("%d%d",&voter[i].party,&voter[i].money);
cnt[--voter[i].party]++;
}
sort(voter,voter+n,cmp);
ll minn=inf;
rep(i,n/m,n+1)//枚举所有可能的得票数
{
memcpy(tcnt,cnt,sizeof(int)*m);
clr(vis,0);
ll sum=0;
for(int j=0;tcnt[0]<=i&&j<n;j++)//枚举所有投票者
//一旦1号候选人得票数大于此得票数就退出循环
{
if(voter[j].party&&tcnt[voter[j].party]>=i)
//如果投的是其他候选人且不小于此投票数
{
tcnt[0]++;
sum+=voter[j].money;
vis[j]=true;
tcnt[voter[j].party]--;
}
}
if(tcnt[0]>i) continue;
//1号候选人得票数大于此得票数,不存在这种情况
for(int j=0;tcnt[0]<i&&j<n;j++)//再次枚举所有投票者
//一旦1号候选人得票数等于此得票数就退出循环
{
if(voter[j].party&&!vis[j])
//如果投的是其他候选人且没被收买过
{
tcnt[0]++;
sum+=voter[j].money;
tcnt[voter[j].party]--;
}
}
if(tcnt[0]==i) minn=min(minn,sum);
//1号候选人得票数等于此得票数
}
cout<<minn<<endl;
return 0;
}