B

B. Soldier and Badges
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
inputCopy
4
1 3 1 4
outputCopy
1
inputCopy
5
1 2 3 2 5
outputCopy
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.

题意:操作最少次数使得序列各不相同。
思路:for跑一遍模拟即可

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int n;
    int a[3000 + 5];
    int num[3000 + 5];
    while(~scanf("%d",&n))
    {
        int sum1 = 0;
        int sum2 = 0;
        for(int i = 0;i < n;i++)
        {
            scanf("%d",&a[i]);
            sum1 += a[i];
        }
        sort(a,a + n);
        sum2 = a[0];
        for(int i = 1;i < n;i++)
        {
            if(a[i] == a[i - 1])
            {
                a[i]++;
            }
            //下面这一行我个人觉得可以不用加,但是加了才能过的。。
            //额,煞笔了,排序后后面的元素>=前面的元素,等于就+1,此 //时有可能大于后面的元素,那么就要对后面的操作进行额外的操作了(+2)
            //我以前的看法是后面的数字要么+1,要么不变,要么+2,如果给出 //全部相等的一段数(1,1,1,1,1),这种情况必须按如下方式处理
            else if(a[i] < a[i - 1])
                a[i] = a[i] + (a[i - 1] - a[i]) + 1;
            sum2 += a[i];
        }
        printf("%d
",sum2 - sum1);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/tomjobs/p/10612584.html