C. Elections ZOJ3715原题
input
standard input
output
standard output
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.
Input
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.
Output
Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections.
Examples
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
Note
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个选民,m个党派,给出n个选民本来的选择和贿赂他的花费,求使自己(1号)当选的最小花费。
n, m的数据范围只有3000
枚举选票,如果有党派的选票>k-1, 先将其贿赂到k-1, 再贪心的选择即可。
#include <bits/stdc++.h>
#define ll long long
#define inf 0x7f7f7f7f
#define ms(x) memset(x, 0, sizeof(x))
#define int long long
using namespace std;
const int N = 3e3+7;
struct node{
int vote, mon, id;
}q[N];
int cnt[N];
int vis[N], book[N];
int n, m;
vector<node>vec;
bool cmp(node a, node b){
return a.mon<b.mon;
}
signed main()
{
ll ans = LONG_LONG_MAX;
ms(cnt);
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
scanf("%d%d",&q[i].vote, &q[i].mon);
q[i].id = i;
cnt[q[i].vote]++;
}
for(int k=max(1LL, cnt[1]); k<=n;k++){
ms(vis);
int need = 0, de = cnt[1];
for(int i=2;i<=m;i++){
if(cnt[i]>k-1){
de += (cnt[i] - (k-1));
if(de>k) break; //这样自然选票数为k自然不成立
vec.clear();
for(int j=0;j<n;j++){
if(q[j].vote == i){
vec.push_back(q[j]);
}
}
sort(vec.begin(), vec.end(), cmp);
for(int j=0;j<cnt[i] - (k-1)&& j<vec.size();j++){
need += vec[j].mon;
vis[vec[j].id] = 1;
}
}
}
if(de>k) continue;
vec.clear();
for(int i=0;i<n;i++){
if(q[i].vote!=1 && !vis[i]){
vec.push_back(q[i]);
}
}
sort(vec.begin(), vec.end(), cmp);
for(int i=0;i<k-de&&i<vec.size();i++){
need += vec[i].mon;
}
ans = min(ans, need);
}
if(ans!=LONG_LONG_MAX)
printf("%lld\n",ans);
else puts("0");
return 0;
}