time limit per test3 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness 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.

【题目链接】:http://codeforces.com/contest/546/problem/B

【题解】



把数字从小到大排序下;

如果有相同的数字,就整体往后移动,每个数字都往后移,直到每个数字都不一样为止;(一格一格地移动);

如果一开始这个数字就被人占据了,则所有的想同数字(len个)都同时往后移动;然后再一个一个往后移动;

移动谁都一样吧.

比较明显的贪心吧.



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int MAXN = 3e3+100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); int n;
int a[MAXN];
bool bo[MAXN*4]; int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);
rep1(i,1,n)
rei(a[i]);
sort(a+1,a+1+n);
LL ans = 0;
rep1(i,1,n)
{
int j = i;
while (j+1<=n && a[j+1]==a[i])
j++;
int len = j-i+1;
int idx = a[i];
while (bo[idx])
{
idx++;
ans += len;
}
bo[idx] = true;
rep1(k,idx+1,idx+len-1)
{
bo[k] = true;
ans+=k-idx;
}
i = j;
}
cout << ans << endl;
return 0;
}

更牛逼的代码。

#include <iostream>
using namespace std; bool v[10000000]; int main() {
int r = 0, n, c;
cin >> n;
while (n--) {
cin >> c;
while (v[c]) {
c++;
r++;
}
v[c] = 1;
}
cout << r;
}

【codeforces 546B】Soldier and Badges的更多相关文章

  1. 【codeforces 546E】Soldier and Traveling

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. 【codeforces 546D】Soldier and Number Game

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【codeforces 546C】Soldier and Cards

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 546A】Soldier and Bananas

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 「CodeForces 546B」Soldier and Badges 解题报告

    CF546B Soldier and Badges 题意翻译 给 n 个数,每次操作可以将一个数 +1,要使这 n 个数都不相同, 求最少要加多少? \(1 \le n \le 3000\) 感谢@凉 ...

  6. 【CodeForces - 546C】Soldier and Cards (vector或队列)

    Soldier and Cards 老样子,直接上国语吧  Descriptions: 两个人打牌,从自己的手牌中抽出最上面的一张比较大小,大的一方可以拿对方的手牌以及自己打掉的手牌重新作为自己的牌, ...

  7. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  8. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  9. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

随机推荐

  1. 一起talk C栗子吧(第九十八回:C语言实例--使用消息队列进行进程间通信二)

    各位看官们,大家好,上一回中咱们说的是使用消息队列进行进程间通信的样例.这一回咱们接着上一回的内容继续说使用消息队列进行进程间通信.闲话休提.言归正转.让我们一起talk C栗子吧! 我们在上一回中介 ...

  2. 97.TCP通信

    运行截图: 客户端 创建通信套接字 //通信套接字,用于创建TCP连接 SOCKET socket_send; 创建tcp通信 //创建tcp通信 socket_send = socket(AF_IN ...

  3. html只能有一个id,并且id的值只能是一个

    1.如果有相同的ID,javascript只会取第一个具有该ID的标签. 2.如果id值有两个,JS只会取到第一个,并不会像class类一样,类名并列就可以同时取到.

  4. GO语言学习(十七)Go 语言类型转换

    Go 语言类型转换 类型转换用于将一种数据类型的变量转换为另外一种类型的变量.Go 语言类型转换基本格式如下: type_name(expression) type_name 为类型,expressi ...

  5. (转)如何启动或关闭数据库的归档(ARCHIVELOG)模式

    转自:http://www.eygle.com/archives/2004/10/oracle_howtoeci.html Oracle数据库可以运行在2种模式下:归档模式(archivelog)和非 ...

  6. 小程序踩坑记- tabBar.list[3].selectedIconPath 大小超过 40kb

    重新启动微信小程序编辑器的时候遇到了这样的一个问题: tabBar.list[3].selectedIconPath 大小超过 40kb 微信小程序开发的过程之中总会出现这样或者那样的错误,需要我们耐 ...

  7. POJ 3278 Catch That Cow(BFS 剪枝)

    题目链接:http://poj.org/problem?id=3278 这几次都是每天的第一道题都挺顺利,然后第二道题一卡一天. = =,今天的这道题7点40就出来了,不知道第二道题在下午7点能不能出 ...

  8. [Angular] New async 'as' syntax and ngIf.. else

    From Anuglar v4 above, we are able to using 'as' with async pipe. This allow as using 'new variable' ...

  9. Android新控件RecyclerView剖析

    传智·没羽箭(传智播客北京校区Java学院高级讲师) 个人简单介绍:APKBUS专家之中的一个,黑马技术沙龙会长,在移动领域有多年的实际开发和研究经验.精通HTML5.Oracle.J2EE .Jav ...

  10. zynq mac地址修改

    工作中遇到多个zynq板子同时位于一个交换机网络中,由于默认mac地址相同,无法进行通信,因此需要对每个板子修改mac地址. 方案:使用uboot配置文件 步骤: 在uboot配置文件中添加及修改下面 ...