Codeforces Round #503 (by SIS, Div. 2) C. Elections(枚举,暴力)

原文地址

C. Elections
time limit per test
2 seconds
memory limit per test
256 megabytes
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 (1n,m30001≤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 (1pim1≤pi≤m, 1ci1091≤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
Copy
1 2
1 100
output
Copy
0
input
Copy
5 5
2 100
3 200
4 300
5 400
5 900
output
Copy
500
input
Copy
5 5
2 100
3 200
4 300
5 800
5 900
output
Copy
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个政党,每个学生有支持的政党,但是如果你给他一些钱,他就可以给你想让他投的党投票,现在想付出最少的钱使得1政党有绝对优势(票数严格大于其他党)。1<=n,m<=30001<=n,m<=3000

  有一种贪心策略是一直收买所需钱最少的学生直到符合条件,但是这样显然是有点问题的,有可能其实只用收买一个收钱多的使得他的政党失败就可以了,但是却收买了许多所需钱虽然少但是无关紧要的人。关键是1号的票数没有确定使得难以贪心,所以考虑枚举最终票数。枚举完票数就开始处理,把每个党超过这个票数且收钱最少的人收买过来,如果这些人都收买完了可是还没有达到预定的票数,就一直收买之前还没有收买过的学生直到人数达标。开long long。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=3300;
const ll inf=0x3f3f3f3f3f3f;
ll n,m;
vector<int>a[maxn];
ll calc(int cnt){
    int left=cnt-a[1].size();//还要多少个人
    ll ans=0;//花费的钱
    int tot=0;//
    int val[maxn];
    for(int i=2;i<=m;i++){
        int tmp=0;//2>=2 0=2-2+1如果减去不够就无穷大
        if(a[i].size()>=cnt)tmp=a[i].size()-cnt+1;
        if(tmp>left)return inf;
        left-=tmp;//需要的人数剪掉
        for(int j=0;j<tmp;j++){
            ans+=a[i][j];
        }
        for(int j=tmp;j<a[i].size();j++){
            val[++tot]=a[i][j];
        }
    }
    sort(val+1,val+tot+1);
    for(int i=1;i<=left;i++)ans+=val[i];
    return ans;

}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    ll ans=inf;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        ll one,two;
        cin>>one>>two;
        a[one].push_back(two);
    }
    for(int i=1;i<=m;i++)sort(a[i].begin(),a[i].end());
    for(int cnt=a[1].size();cnt<=n;cnt++)
        ans=min(ans,calc(cnt));
    cout<<ans<<endl;
    return 0;
}

  

原文地址:https://www.cnblogs.com/luowentao/p/9462738.html