Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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.

Sample Input

Input
4
1 3 1 4
Output
1
Input
5
1 2 3 2 5
Output
2

Hint

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.

题意:

军官要给手下的n个士兵发奖章,给定n个整数,可在每个数上多次加1,要求最终结果所有n个数均不相等,求最少要加多少次1。

首先我们设一个数组a来储存所有n个数字,按升序排序。对a数组扫一遍,如果a[i]==a[i+1],则需要改变数的大小。那么我们怎么知道要加多少个一呢?在本题中我设了一个ans标记用来表示比a[i+1]大且不存在a中的最小整数,ans-a[i+1]即为我们要加1的个数。

注意:每次加1之后整个a数组就会发生改变,为此我定义了一个数组b来表示变化后的a数组,以便于求ans。

附AC代码:

 #include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; int a[],b[]; int main(){
int n,t;
cin>>n;
for(int i=;i<n;i++){
cin>>a[i];
b[i]=a[i];//b数组来表示变化后的a数组
}
sort(a,a+n);//排序
sort(b,b+n);
int ans=a[];
int sum=;
for(int i=;i<n;i++){
if(a[i]==ans){
ans++;
}
}
for(int j=;j<n;j++){
if(a[j]==a[j+]){
sum+=(ans-a[j+]);
b[j+]=ans;//变化
ans++;
}
else if(a[j+]>ans){//保证ans不小于a[j+1]
ans=a[j+];
}
for(int i=;i<n;i++){
if(b[i]==ans){
ans++;
}
}
}
cout<<sum; return ;
}

B - Soldier and Badges的更多相关文章

  1. CF Soldier and Badges (贪心)

    Soldier and Badges time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  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. 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges

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

  4. CF 546 B Soldier and Badges(贪心)

    原题链接:http://codeforces.com/problemset/problem/546/B 原题描述: Soldier and Badges Colonel has n badges. H ...

  5. Codeforces Round #304 (Div. 2) B. Soldier and Badges【思维/给你一个序列,每次操作你可以对一个元素加1,问最少经过多少次操作,才能使所有元素互不相同】

    B. Soldier and Badges time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

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

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

  7. 题解 CF546B Soldier and Badges

    CF546B Soldier and Badges 简单的贪心qwq 排个序,如果当前数与之前的数相重,已经用过,则加到一个之前没有用过的数 #include<cstdio> #inclu ...

  8. Soldier and Badges (set的检索简单运用)

    Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolne ...

  9. 【codeforces 546B】Soldier and Badges

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

随机推荐

  1. Oracle EBS - 工单状态

    Job status update 1. Job的几种状态 unreleased --未核发 released--已核发 complete --完成 complete no charges--完成不计 ...

  2. 在Ubuntu 10.10下安装JDK配置Eclipse及Tomcat

    1.安装JDK 1.1.到官网下载相关的JDK 这里下载的是 jdk-6u23-linux-i586.bin. 下载地址:http://www.oracle.com/technetwork/java/ ...

  3. gameplay理解

    Camera视角:确定显示的视场及视角. Game:显示的基类.静态单例模式.但是获取方式很奇怪. Game::getInstance得到的是__gameInstance,但是__gameInstan ...

  4. XMLHttpRequest是什么、如何完整地运行一次GET请求、如何检測错误。

    var xmlhttp; function LoadXmlDoc(url){ xmlhttp = null; if(window.XMLHttpRequest){ //code for all new ...

  5. GCD编程(封装GCD)

    //GCDGroup 类 @interface GCDGroup : NSObject @property (strong, nonatomic, readonly) dispatch_group_t ...

  6. cin,和几个get函数的用法

    1.cin.get(字符变量名):用来接收字符 ch = cin.get(); cin.get(ch); 以上两者均可以 2.cin.get(字符数组名,接收字符数目)用来接收一行字符串,可以接收空格 ...

  7. Objective-C之成魔之路【10-继承性】

    郝萌主倾心贡献.尊重作者的劳动成果.请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主.捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 继承性是面 ...

  8. The most widely used name server software: BIND

    https://www.isc.org/downloads/bind/ The most widely used name server software: BIND BIND is open sou ...

  9. HDU 1242 rescue (优先队列模板题)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  10. LINQ解决依据某个字段去重

    想要List结果反复 的数据非常easy.仅仅要.Dinstinct()就好了 可是假设想要依据某个字段去除反复的数据,上面的方法就帮不上忙了.我们须要重写一个方法.直接上样例吧 [Serializa ...