Codeforces Round #304 (Div. 2) B. Soldier and Badges【思维/给你一个序列,每次操作你可以对一个元素加1,问最少经过多少次操作,才能使所有元素互不相同】
3 seconds
256 megabytes
standard input
standard output
Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has acoolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin.
For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors.
Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness.
First line of input consists of one integer n (1 ≤ n ≤ 3000).
Next line consists of n integers ai (1 ≤ ai ≤ n), which stand for coolness factor of each badge.
Output single integer — minimum amount of coins the colonel has to pay.
4
1 3 1 4
1
5
1 2 3 2 5
2
In first sample test we can increase factor of first badge by 1.
In second sample test we can increase factors of the second and the third badge by 1.
【题意】:给你一个序列,每次操作你可以对一个元素加1,问最少经过多少次操作,才能使所有元素互不相同
【分析】:排序,求和。有相邻相等的,后面的+1。后面的小于前面的,后面的变为前面的数+1,再次求新和。新和-旧和为所求。
【代码】:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm> using namespace std;
int sum1,sum2;
int a[3000];
int main()
{
int n, i, j; scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
sum1+=a[i];
} sort(a, a + n);
sum2 = a[0];
for (i = 1; i < n; i++)
{
if(a[i]==a[i-1])
a[i]++; else if(a[i]<a[i-1])
a[i] = a[i-1]+1; sum2+=a[i];
}
printf("%d\n", sum2-sum1);
return 0;
}
/*
1 2 2 3 5=13
1 2 3 4 5=15
15-13=2
1 1 1 2 5
1 2 3 4 5 */
Codeforces Round #304 (Div. 2) B. Soldier and Badges【思维/给你一个序列,每次操作你可以对一个元素加1,问最少经过多少次操作,才能使所有元素互不相同】的更多相关文章
- 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
		
题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...
 - Codeforces Round #304 (Div. 2) B. Soldier and Badges 水题
		
B. Soldier and Badges Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/54 ...
 - DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
		
题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...
 - queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards
		
题目传送门 /* 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 queue容器:模拟上述过程,当次数达到最大值时判断为-1 */ #include <cstdio&g ...
 - 水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas
		
题目传送门 /* 水题:ans = (1+2+3+...+n) * k - n,开long long */ #include <cstdio> #include <algorithm ...
 - 数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game
		
题目传送门 /* 题意:这题就是求b+1到a的因子个数和. 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 */ /***************** ...
 - Codeforces Round #304 (Div. 2)(CF546D) Soldier and Number Game(线性筛)
		
题意 给你a,b(1<=b<=a<=5000000)表示a!/b!表示的数,你每次可以对这个数除以x(x>1且x为这个数的因子)使他变成a!/b!/x, 问你最多可以操作多少次 ...
 - Codeforces Round #304 (Div. 2) D. Soldier and Number Game 数学 质因数个数
		
D. Soldier and Number Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...
 - Codeforces Round #304 (Div. 2) E. Soldier and Traveling 最大流
		
题目链接: http://codeforces.com/problemset/problem/546/E E. Soldier and Traveling time limit per test1 s ...
 
随机推荐
- C#窗体阴影
			
/// <summary> /// 边框阴影 /// </summary> protected override CreateParams CreateParams { get ...
 - reac-native + typescript 的环境搭建
			
一. RN-TS环境搭建 . 安装RN脚手架 yarn add create-react-native-app -g yarn global add typescript . 创建项目文件夹 crea ...
 - window环境下Python+OpenCV配置
			
最近开始学习OpenCV来进行计算机视觉实验,选择了Python作为实验语言,工欲善其事,必先利其器.先总结下安装配置. 现在opencv目测只支持Python2.7X版本的,还依赖于numpy和ma ...
 - C/C++ 信号量 CreateSemaphore 用法
			
HANDLE CreateSemaphore( LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, // SD LONG lInitialCount, // in ...
 - react使用swiper,解决添加点击事件首位图片点击失效,解决轮播按钮被覆盖问题
			
JS部分 createSwiper1() { var option = { // slidesPerView: 5, slidesPerView: 3, centeredSlides:true, }; ...
 - 全栈之路-杂篇-前端Http请求封装优化
			
在项目开发过程中,代码的封装是很有必要的,我觉得这是程序员进阶的一个重要的技能,不会封装代码,你的代码看起来乱的一批,基本上不能维护,像一次性塑料袋一样,用完一次就失去了价值,这同时也会无缘无故的增加 ...
 - JavaSE_09_Map
			
1.1 概述 Collection中的集合,元素是孤立存在的,向集合中存储元素采用一个个元素的方式存储. Map中的集合,元素是成对存在的(Key---Value).每个元素由键与值两部分组成,通过键 ...
 - 写js过程中遇到的一个bug
			
<div class="func_Div" id="xxcx"><span>信息查询</span> ...
 - Java面试总结-基础篇2
			
1. mvn的dependency-management dependency-management是声明依赖,不实际引入,主要用于在父依赖中统一各依赖的版本,否则,各个子模块在引用同一依赖时,难免会 ...
 - Junit5的依赖添加及RunWith(SpringJUnit4ClassRunner.class)注解使用
			
首先Junit5依赖应该配置为 <dependency> <groupId>org.junit.jupiter</groupId> <artifactId&g ...