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 ;
}

最新文章

  1. delphi 弹出选择目录窗口
  2. Linux下select&amp;poll&amp;epoll的实现原理(一)
  3. centos 7.0VI命令 和固定内网IP 查看外网IP
  4. 浅谈C#抽象方法、虚方法、接口
  5. 《C#并行编程高级教程》第9章 异步编程模型 笔记
  6. bootstrap悬浮顶部或者底部
  7. bzoj 3611[Heoi2014]大工程 虚树+dp
  8. Rehat一键安装mysql脚本和备份数据库脚本
  9. Spring MVC 过滤静态资源访问
  10. PHP连接mysql数据库进行增删改查--删除
  11. Mysql的学习笔记(一)--检验数据库是否安装成功以及一些入门的简单的dos命令
  12. 【转】sed单行命令大全
  13. 深入理解JVM(七)——性能监控工具
  14. 『没有上司的舞会 树形DP』
  15. Docker 删除所有无名称的镜像(悬空镜像)
  16. equals和contains的区别
  17. TP的di
  18. Spark中的Spark Shuffle详解
  19. (最小生成树) Arctic Network -- POJ --2349
  20. 一、SDWebImage分析--库处理流程分析

热门文章

  1. ORACLE 内部原理
  2. Java利用Mybatis进行数据权限控制
  3. NSURLConnection和NSMutableURLRequest 实现同步、异步请求
  4. android 打开浏览器指定网页
  5. [原创]安装Ubuntu Server 14.04后
  6. Java基础 笔记(四)
  7. Android Studio——gradle同步出错:MALFORMED
  8. CSS 的导入方式 (link or import ?)
  9. 【转载】读懂IL代码就这么简单(二)
  10. SVN——库合并