Codeforces 574 A. Bear and Elections

题目链接: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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值