题目链接:http://codeforces.com/problemset/problem/574/A
题 意:给你n个人,第二行有n个数,每个数代表第i个人的选票数量。现在第1号人想要赢得选举。所以在选票数量上1号候选人的选票数量
必须严格大于其他候选人的选票数。如果1号候选人的选票不能让他被选举上,他就会去偷其他人的。直到他的票数最多。问:他最少需要偷多少张?
(偷的选票可以来自其他的任何人)
思 路:可以定义一个优先队列,将2到n的票数入队,将队内最大的值与第一个比较,若大于等于,则最大值减一、第一个加一。
代码如下:#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> #include <queue> using namespace std; typedef __int64 LL; struct cmp{ bool operator() ( const int a, const int b ) const{ return a < b; } }; int main() { LL n; while( scanf ( "%I64d", &n ) != EOF ) { int my; priority_queue< int, vector<int>, cmp > q; scanf ( "%d", &my ); for( int i = 1; i < n; i ++ ) { int x; scanf ( "%d", &x ); q.push( x ); } int ans = 0; while( my <= q.top() ) { int y; y = q.top(); q.pop(); my++; y--; ans++; q.push( y ); } printf( "%d\n", ans ); } return 0; }