模拟赛#1补题 CodeForces 574A Bear and Elections(模拟+)

【题目链接】:click here~~

A. Bear and Elections
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 getai 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.

Sample test(s)
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.


【题目大意】:Limak(默认是第一个人)想要通过贿赂其他选民来提高自己的票数,给出n个人的票数,问最少贿赂多少个人使得剩下的人的票数都小于他

【思路】优先队列或者数组模拟

solution one:

代码:


/*
* Problem: CodeForces 574A
* Running time: 15MS
* Complier: G++
* Author: herongwei
* Create Time: 20:00 2015/10/13 星期二
*/
#include <stdio.h>
#include <string.h>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
priority_queue<int >q;
int main()
{
    //freopen("1.txt","r",stdin);
    int n;cin>>n;
    int x;cin>>x;
    for(int i=2; i<=n; ++i){
        int y;cin>>y;
        if(y>=x)q.push(y);
    }
    int bribe=0;
    while(!q.empty()){
        int t=q.top();q.pop();
        if(t>=x){
            t-=1;
            x+=1;
            q.push(t);
            bribe++;
        }
    }
    cout<<bribe<<endl;
    return 0;
}

solution two:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1e5+10;
int arr[maxn]={0};
int main()
{
    //freopen("1.txt","r",stdin);
    int t;cin>>t;
    for(int i=1; i<=t; ++i)
    cin>>arr[i];
    int maxx=0,max_idex=0;
    int bribe=0;
    while(max_idex!=1)
    {
        maxx=0;
        max_idex=0;
        for(int i=1; i<=t; ++i)
        {
            if(arr[i]>=maxx)
            {
                maxx=arr[i];
                max_idex=i;
            }
        }
        if(max_idex!=1)
        {
            arr[1]+=1;
            arr[max_idex]-=1;
            bribe++;
        }
    }
    cout<<bribe<<endl;
    return 0;
}


你可能感兴趣的:(codeforces,比赛)