Bear and Elections CodeForces - 574A(优先队列)详解

Bear and Elections CodeForces - 574A

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

Input

5
5 1 11 2 8

Output

4

Input

4
1 8 8 8

Output

6

Input

2
7 6

Output

0

Note
In the first sample Limak has 5 votes. One of the ways to achieve victory is to bribe 4 citizens who want to vote for the third candidate. Then numbers of votes would be 9, 1, 7, 2, 8 (Limak would have 9 votes). Alternatively, Limak could steal only 3 votes from the third candidate and 1 vote from the second candidate to get situation 9, 0, 8, 2, 8.
In the second sample Limak will steal 2 votes from each candidate. Situation will be 7, 6, 6, 6.
In the third sample Limak is a winner without bribing any citizen.

题意:

就是一只熊要选市长,他要用糖果贿赂选民,让他成为市长

思路:

思路很简单,因为总人口是不变的,所以,他每贿赂过来一个人,别人就得少一个人,要让他成为市长,就得跟最多的人比较,那么问题就解决了,他每次都要贿赂最高的那个人的选民,让最高的人的选民减少,他的选民增加。那怎样每次都找到最多的那个人呢?每次for循环找最大值?那样复杂度太高。那这样就要用到一种特殊的队列——优先队列(特别强大的队列);所谓优先队列呢,也是队列的一种,当你入队的时候,已经给你排好序了
下面先讲一下优先队列,懂了的话,这题自然就解决了。

优先队列的头文件跟队列是一样的了,只不过它的取名方式要变一下:
一个优先队列声明的基本格式是:
priority_queue<结构类型> 队列名;

#include<stdio.h>
#include<queue>
using namespace std;
priority_queue<int>q;

这样一个结构类型为int,队列名为q的有限队列就取好了。那么这个优先队列怎么用呢?先看一下优先队列的基本操作

基本操作

q.size();//返回q里元素个数
q.empty();//返回q是否为空,空则返回1,否则返回0
q.push(k);//在q的末尾插入k
q.pop();//删掉q的第一个元素
q.top();//返回q的第一个元素

关键的就是这个q.top,太强大了。

#include<cstdio>
#include<queue>
using namespace std;
priority_queue <int> q;
int main()
{ 
 q.push(10);
 q.push(8);
 q.push(12);
 q.push(14);
 q.push(6);
 while(!q.empty())
 {
  printf("%d ",q.top());//每次输入这个q.top,看看到底是什么,然后pop;
  q.pop(); 
 }
 return 0;
}

按正常队列先进先出的话,输出应该是10 8 12 14 6 。 优先队列的话就是在这里插入图片描述
这就凸显出优先队列的强大了,已经排好序了,q.top是这个队列的最大值
那么说到这里,小熊选市长的问题相信大部分人都已经有思路了,下面直接看代码,有详细注释

#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
int main()
{
 int n;
 int a[110];
 int ans; 
 while(~scanf("%d",&n))//多组输入 
 {
  int count=0;   //小熊需要付出的糖果个数 
  priority_queue<int>q;//定义有限队列 
  while(!q.empty())//初始化队列 
   q.pop();
  scanf("%d",&ans);//ans就相当于小熊开始的时候的选民个数 
  for(int i=0;i<n-1;i++)//那现在就剩下n-1个竞争对象了 
  {
   scanf("%d",&a[i]);
   q.push(a[i]);//每次输入都入队 
  }
  int temp=q.top();//用temp来表示竞争对象中的最大值 
  while(ans<=temp)//当自己的选民小于最大值时 
  { 
   count++;//付出的糖果数加一 
   ans++;//自己的选民加一 
      q.pop();//将最大的pop出 
      temp--;//最大的人的选民被贿赂一个,减少一个 
      q.push(temp);//将减少后的值入有限队列 
      temp=q.top();//在取出重新排好的队列的最大值进行循环  
  }
  printf("%d\n",count);
 }
 return 0;
}

这样就会计算出他要付出的糖果数了。
不过现实生活中还是不要贿赂,要公平竞争

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值