CF 546 B Soldier and Badges(贪心)

原题链接:http://codeforces.com/problemset/problem/546/B

原题描述:

Soldier and Badges

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.

Example

Input
4
1 3 1 4
Output
1
Input
5
1 2 3 2 5
Output
2
 题目感觉很水啊,数量最多才3000个,就算3000个每个都是3000增加后的最大值也不会超过6000,所以我开了个10005的数组;先将每个读入,然后统计1-10000之间每个数字的个数,比如说3000这个数字有2个那么b[3000]值为3,然后让b[3001]增加2,b[3000]变为1,同时需要增加的总量增加2以此类推统计增加的总量即可,也不知道别人看不看得懂。。。。看看代码应该能理解
AC代码如下:
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL;

int a[10005],b[10005];

int main()
{
    int n,sum=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        b[a[i]]++;
    }
    for(int i=1;i<=10005;i++)
    {
        if(b[i]>1)b[i+1]+=b[i]-1,sum+=b[i]-1,b[i]=1;
    }
    cout<<sum<<endl;
    return 0;
}

  

原文地址:https://www.cnblogs.com/teble/p/7202686.html