7.18

来源:互联网 发布:windows 平板电脑推荐 编辑:程序博客网 时间:2024/05/29 02:45
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 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 test(s)
input
41 3 1 4
output
1
input
51 2 3 2 5
output
2贪心算法:先排一次序,然后找相同的,是相同的++,在排序在返复这样的操作,但是这样肯定会超时,反复排序! (注意有几个>2个数相同的情况)所以  直接拍一次序,要是后面的比前面的的小或等于,就执行:a[i]=a[i-1]+1;还看到过别人的另外一种做法,直接用该数组的元素和对应的下标比较源代码链接:
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int main(){    int a[3000];    int i,j,k;    int sum1,sum2;    sum1=sum2=0;    int n;    cin>>n;    for (i=0;i<n;i++)        {            scanf("%d",&a[i]);            sum1+=a[i];        }    sort(a,a+n);    k=0;        for (i=0;i<n;i++)        {            if (a[i+1]==a[i]  && i<n)            {                a[i+1]++;            }            if (a[i+1]<a[i] && i<n)//用来判断这一数组元素有两个以上相等的情况            {                a[i+1]+=a[i]-a[i+1]+1;            }            sum2+=a[i];        }    printf("%d\n",sum2-sum1);    return 0;}