[Codeforces 1019.A] Elections(枚举答案)
A. Elections
time limit: per test2 seconds
memory limit: per test256 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 — $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 $i$-th voter $c_i$ 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.
Input
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 $n$ lines contains two integers $p_i$ and $c_i$ $(1≤p_i≤m, 1≤c_i≤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$.
Output
Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections.
Examples
input | output |
---|---|
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 |
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 $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$ 号政党最终得票 $k$,那么其余政党得票必须小于 $k$,所以对于现在得票大于等于 $k$ 的政党,我们把他py交易到小于 $k$;如果这之后我们的得票还不足 $k$,就按价格从小到大py到 $k$。(具体看代码注释)
时间复杂度 $O(nm)$
事实上,codeforces458C(链接)是此题加强版,暴力枚举 $k$ + 线段树可过。(口胡 qwq)
不过据信,花的钱与 $k$ 的函数是单峰的?然而并不知道为什么。(欢迎留言讨论)
Code
1 |
|