zoukankan      html  css  js  c++  java
  • CodeForces 546B

    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.

    题意很简单,我用数据来解释题意把 1 3 1 4,不可以有相同的,加最少的数使各个数据不同

    先排序下,前后相等,后者加1,后者小于前者,加后者减前者加1

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<cstring>
     6 using namespace std;
     7 int main()
     8 {
     9     int n;
    10     int i,j;
    11     int a[3005];
    12     while(cin>>n)
    13     {
    14         memset(a,0,sizeof(a));
    15         for(i=1;i<=n;i++)
    16             cin>>a[i];
    17         sort(a+1,a+n+1);
    18         int sum=0;
    19         for(i=2;i<n;i++)
    20         {
    21             if(a[i]==a[i-1])
    22             {
    23                 a[i]=a[i]+1;
    24                 sum++;
    25             }
    26             if(a[i]>=a[i+1])
    27             {
    28                 sum=sum+a[i]-a[i+1]+1;
    29                 a[i+1]=a[i]+1;
    30             }
    31         }
    32         cout<<sum<<endl;
    33     }
    34 }
  • 相关阅读:
    C语言I博客作业10
    C语言I博客作业09
    C语言I博客作业08
    背景图
    C语言II作业01
    C语言寒假大作战04
    C语言寒假大作战03
    C语言寒假大作战02
    C语言寒假大作战01
    C语言I博客作业12
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/5699560.html
Copyright © 2011-2022 走看看