Codeforces Round #318 (Div. 2) A. Bear and Elections

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wbwal159/article/details/48099097

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

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.




第一次打CF,只做了两道题大哭,B题没找到哪错了,明天再继续改吧。

这道题就是要让Limak得到的选票最多,我的做法是将其他候选人进行排序,每次都和票数最高的那个人进行比较,不满足结束条件的话就继续贿赂。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#define MAX 
#define LL long long
using namespace std;

int can[110];

int main()
{
/*	freopen("1.txt" , "r" , stdin);  */
	int i,j,k,m,n,cou,limak,flag;
	while(~scanf("%d" , &n)){
		scanf("%d" , &limak);			//先记录下Limak的票数 
		flag = 1;
		for(i = 1;i < n;i++){
			scanf("%d" , &can[i]);
			if(flag && can[i] >= limak){
				flag = 0;
			}
		}
		if(flag){					//如果没有比Limak的票数多的人,那他直接就赢了 
			printf("0\n");
			continue;
		}
		sort(can + 1 , can + n );	//进行第一次排序 
		can[0] = 0;
		cou = limak;
		i = n - 1;
		while(1){
			if(limak > can[i]){			//排序之后最大的票数在最后一个,如果比最后一个还要高,那就是已经比所有人都高了,任务完成 
				break;
			}
			limak++;can[i]--;			//Limak的票增加一个,最大的那个要减少一个 
			if(can[i] < can[i-1]){		//对变动过的序列重新排序 
				sort(can + 1 , can + n);
			}
		}
		printf("%d\n" , limak - cou);
	}
	return 0;
} 

最开始还担心一直用排序会TLE,但一时没想到其它方法,就暴力的做了,没想到还真A了。 大笑


猜你喜欢

转载自blog.csdn.net/wbwal159/article/details/48099097