Bear and Elections
Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland.
There are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th candidate would get ai votes. Limak is candidate number 1. To win in elections, he must get strictly more votes than any other candidate.
Victory is more important than everything else so Limak decided to cheat. He will steal votes from his opponents by bribing some citizens. To bribe a citizen, Limak must give him or her one candy - citizens are bears and bears like candies. Limak doesn't have many candies and wonders - how many citizens does he have to bribe?
Input
The first line contains single integer n (2 ≤ n ≤ 100) - number of candidates.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1000) - number of votes for each candidate. Limak is candidate number 1.
Note that after bribing number of votes for some candidate might be zero or might be greater than 1000.
Output
Print the minimum number of citizens Limak must bribe to have strictly more votes than any other candidate.
Examples
5
5 1 11 2 8
4
4
1 8 8 8
6
2
7 6
0
大概题意:把其他人的票分给第一人,使得第一个人的票刚好比其他人得票多。用最大优先队列来做,每次通过取出顶部元素(一定是最大的)来与第一个人比较,如果大就将它减一,在放入队列中,
给第一个人加1;反之就说明已分配好了。
priority_queue<int,vector<int>,less<int> >,最大优先队列,从大到小排序
priority_queue<int,vector<int>,greater<int> >,最小优先队列,从小到大排序
1 #include<iostream> 2 #include<queue> 3 using namespace std; 4 int main() 5 { 6 priority_queue<int,vector<int>,less<int> > q;//最大优先队列 7 int n,x; 8 cin>>n>>x; 9 for(int i=1;i<n;i++) 10 { 11 int a; 12 cin>>a; 13 q.push(a); 14 } 15 int xx=x; 16 while(1) 17 { 18 int k=q.top(); 19 q.pop(); 20 if(xx>k) 21 break; 22 else 23 { 24 xx++; 25 q.push(--k); 26 } 27 } 28 cout<<xx-x<<endl; 29 return 0; 30 }

浙公网安备 33010602011771号