A. Bear and Elections
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?
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.
Print the minimum number of citizens Limak must bribe to have strictly more votes than any other candidate.
5
5 1 11 2 8
4
4
1 8 8 8
6
2
7 6
0
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.
思路应该是每次寻找数组中的最大的一个数减去1
第一个数加一当第一个数最大的时候且用的最小
数组模拟
#include <cstdio>
#include <algorithm>
using namespace std;
#define N 10000
int arr[N];
int main()
{
int n;
scanf("%d",&n);
for(int i = 0;i < n; i++) {
scanf("%d",&arr[i]);
}
int ans = arr[0];
while(true) {
int k = 0;
for(int i = 1;i < n; i++) {
if(arr[i] >= arr[k]) k = i;
}
if(k==0) break;
else {
arr[k]--;
arr[0]++;
}
}
printf("%d\n",arr[0]-ans);
return 0;
}
优先队列(完全没想到这种写法23333
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int main()
{
priority_queue<int> fq;
int n, ans;
scanf("%d%d",&n,&ans);
int k;
for(int i = 1;i < n; i++) {
scanf("%d",&k);
fq.push(k);
}
int m = 0;
while(fq.top()>=ans) {
ans++;
m++;
int z = fq.top();
fq.pop();
fq.push(z-1);
}
printf("%d\n",m);
return 0;
}
本文介绍了一个有趣的问题:一只名叫Limak的熊为了赢得选举,必须获得比其他竞争对手更多的选票。Limak通过贿赂选民的方式来增加自己的选票数量。文章探讨了如何用最少的糖果贿赂尽可能多的选民,确保Limak获胜。

121

被折叠的 条评论
为什么被折叠?



