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. git-flow 工作流 备忘清单

    关于 git-flow 是一个 git 扩展集,按 Vincent Driessen 的分支模型提供高层次的库操作. 查看详情 ★ ★ ★ 这个备忘清单展示了 git-flow 的基本操作和效果. ★ ...

  2. Java中的反射机制,利用反射访问私有

    利用反射,首先是Class对象的获取,之后是Method和Field对象的获取. 以Method为例,从文档中可以看到: getMethod()方法返回的是public的Method对象, 而getD ...

  3. google 集群计算的3大基础设施

    1.  GFS  分布式文件系统 2.  map-reduce 分布式计算框架 3. bigtable 海量key-value的存储 (开源实现:Hypertable)

  4. 搭建企业内部DNS服务器,docker 部署内部 dnsmasq

    获取镜像 docker pull jpillora/dnsmasq 配置域名 # http://oss.segetech.com/intra/srv/dnsmasq.conf #log all dns ...

  5. Microsoft Dynamics CRM Server 2013软件安装要求

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveV9mMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  6. opencms 安装出现以下的问题:Your &#39;max_allowed_packet&#39; variable is set to less than 16777216 Byte (16MB).

    一.问题 在安装opencms是会出现例如以下错误: MySQL system variable 'max_allowed_packet' is set to 1048576 Byte (1MB). ...

  7. Java学习之String StringBuffer StringBuilder区别

    1.String:对象长度是不可变的,是线程安全. 2.StringBuffer:对象长度是可变的,是线程安全. 3.StringBuilder:对象长度是可变,不是线程安全.

  8. 随着ScrollView的滑动,渐渐的运行动画View

    今天是实现了一个小功能的东西.看看效果图: 实现方式: 1.自己定义ScrollView   复写onScrollChange方法,来计算滑动的位置. 2.自己定义接口,通过接口来在ScrollVie ...

  9. HDU 2955 Robberies(01背包变形)

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  10. 一篇很好的讲解SIFT算法的文章

    http://blog.csdn.net/zddblog/article/details/7521424