Codeforces #318 (Div. 2 A. Bear and Elections (模拟/优先队列
来源:互联网 发布:js 直接定义对象 编辑:程序博客网 时间:2024/05/06 22:15
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 10000int 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;}
- Codeforces #318 (Div. 2 A. Bear and Elections (模拟/优先队列
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A. Bear and Elections(优先队列)
- Codeforces Round #318 574A Bear and Elections(优先队列)
- Codeforces Round #318 (Div. 2) A. Bear and Elections
- Codeforces Round #318 (Div. 2) A - Bear and Elections 模拟暴力枚举
- 在青岛穷游打的cf codeforces Round #318 (Div. 2) A.Bear and Elections
- Codeforces Round #318 A. Bear and Elections
- CodeForces 574A - Bear and Elections
- Codeforces 574 A. Bear and Elections
- Codeforces 574 A. Bear and Elections
- Bear and Elections CodeForces
- A. Bear and Elections
- 模拟赛#1补题 CodeForces 574A Bear and Elections(模拟+)
- Codeforces 639A Bear and Displayed Friends (优先队列)
- Codeforces Round #316 (Div. 2)570A Elections(模拟)
- Codeforces Round #195 (Div. 2) / 336A Vasily the Bear and Triangle(模拟&数学)
- Codeforces Round #356 (Div. 2)-A. Bear and Five Cards(模拟)
- Codeforces Round #318 574C Bear and Elections(脑洞)
- 返回上一层常用方法
- 浅谈概率
- 详解C语言中const关键字的用法
- 选择排序
- STM32的时钟系统RCC详细整理
- Codeforces #318 (Div. 2 A. Bear and Elections (模拟/优先队列
- Android中的回调
- android生成分享长图并且添加全图水印
- Activity和Fragment通信
- 欢迎使用CSDN-markdown编辑器
- 模块效率篇:实现LCD模块的流程化开发
- Ant工具的使用
- 15. 3Sum
- git怎样删除未监视的文件untracked files