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 — nn and mm 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 ii-th voter cicibytecoins 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 nn and mm (1≤n,m≤30001≤n,m≤3000) — the number of voters and the number of parties respectively.
Each of the following nn lines contains two integers pipi and cici (1≤pi≤m1≤pi≤m, 1≤ci≤1091≤ci≤109) — 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 11.
Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections.
1 2
1 100
0
5 5
2 100
3 200
4 300
5 400
5 900
500
5 5
2 100
3 200
4 300
5 800
5 900
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 33, 44 and 55 get one vote and party number 22 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.
题意:给出n个人投票,开始每个人都有一个选好的人,如果我们要使它换票就必须支付他所给的价格,我们要使1号选手赢得比赛,问我们支付最少的钱是多少来使一号赢得胜利
思路: 我们考虑到纯贪心有太多种情况,题目所给的数据也是3000,说明我们可以使用n^2以至更高的算法,我们考虑1号选手获胜的状态,就是他是以几票来获得胜利的
枚举获胜状态,然后我们取最优的
给一个例子 1:0票 2:3票 3:3票 4:2票 5 :1票
1号选手1票获取胜利 不存在
2票获取胜利 不存在
3票获取胜利 如果我们要三票赢2号和3号,必须要比2号3号多一票才可以,但是我们总共只买三票,既然我们不能增多自己,只能削弱对手,
其中一票在2号这里买,那么他就会降为2票,3号同理,然后剩下一票,买价格最低的即可
然后四票五票获取胜利的方法同理,我们只要取花费价格最少的状态即可
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; struct sss { int id; int cost; }a[3001]; int f[3001]; int g[3001]; int cmp(struct sss x,struct sss y) { return x.cost<y.cost; } int main() { int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d%d",&a[i].id,&a[i].cost); f[a[i].id]++;//计入最开始的投票情况 } long long sum=999999999999,sum2=0,ans; sort(a+1,a+n+1,cmp);//按价格排好序 for(int i=f[1];i<=n;i++)//枚举获胜状态 { sum2=0; ans=0; for(int j=2;j<=m;j++) { if(f[j]>=i) { g[j]=f[j]-i+1;//记录要赢得此选手要在他这买几票 sum2+=g[j];//记录总共要在比他高的选手这买几票 } else g[j]=0; } if(i-f[1]<sum2) continue;//如果我在比他高的选手这买的票比我总共买的还多说明情况不存在 sum2=i-f[1]-sum2;//记录要在比我低的选手这买几票 for(int j=1;j<=n;j++) { if(g[a[j].id]!=0)//比我高 { g[a[j].id]--; ans+=a[j].cost; } else{ if(sum2!=0&&a[j].id!=1)//比我低,并且我还需要买票 { sum2--; ans+=a[j].cost; } } } sum=min(sum,ans);//取最优 } cout<<sum; }