1. 程式人生 > >Codeforces Round #503 (by SIS, Div. 2) C. Elections

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

不能 情況 記錄 format fir des org return xmlns

C. 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 —

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
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個人投票,開始每個人都有一個選好的人,如果我們要使它換票就必須支付他所給的價格,我們要使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;
}

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