B. Soldier and Badges
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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

Output single integer — minimum amount of coins the colonel has to pay.

Examples
input
4
1 3 1 4
output
1
input
5
1 2 3 2 5
output
2
Note

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,问最少经过多少次操作,才能使所有元素互不相同】的更多相关文章

  1. 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges

    题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...

  2. 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 ...

  3. DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game

    题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...

  4. queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards

    题目传送门 /* 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 queue容器:模拟上述过程,当次数达到最大值时判断为-1 */ #include <cstdio&g ...

  5. 水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas

    题目传送门 /* 水题:ans = (1+2+3+...+n) * k - n,开long long */ #include <cstdio> #include <algorithm ...

  6. 数学+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;再来一个前缀和 */ /***************** ...

  7. 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, 问你最多可以操作多少次 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. js 引入Vue.js实现vue效果

    拆分组件为单个js见:https://www.jianshu.com/p/2f0335818ceb 效果 html <!DOCTYPE html> <html> <hea ...

  2. js 实现音频播放与暂停

    html: <script src="js/jquery-2.1.3.min.js"></script> <div id="soundIco ...

  3. Activiti流程定义语言

    1.流程(process) bpmn文件一个流程的根元素.一个流程就代表一个工作流. 2.顺序流(sequenceFlow) 顺序流是连接两个流程节点的连线,代表一个节点的出口.流程执行完一个节点后, ...

  4. linux 显示ip地址小工具-nali

    1.下载软件包 wget http://qqwry.googlecode.com/files/nali-0.1.tar.gz 2.安装 tar -zxvf nali-0.2.tar.gz cd nal ...

  5. php连接数据库插入数据

    <form action="updata.php" method="post"> 姓名:<input type="text" ...

  6. BM线性递推

    #include<bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #defi ...

  7. [转] Linux 句柄是什么 ?

    源地址:http://www.blogjava.net/shijian/archive/2012/04/06/373463.html 1.句柄就是一个标识符,只要获得对象的句柄,我们就可以对对象进行任 ...

  8. jquery的each()遍历和ajax传值

    页面展示 JS代码部分 /*功能:删除选中用户信息数据*/ function delUser(){ $("#delU").click(function(){ var unoStr ...

  9. [转载] OpenCV2.4.3 CheatSheet学习(四)

    五.数据的输入和输出 1. 将数据写入YAML(或XML) 注意,在OpenCV中,无论读写,文件的格式均由指定的后缀名确定.示例: FileStorage fs("test.yml&quo ...

  10. oracle创建新的连接(表空间?数据库?)

    一.创建用户名密码 create user username identified by password    --username 是用户名:password 是密码 二.给用户附权.撤权 gra ...