第一次比赛的 C题 (好后面才补的....) CodeForces 546B
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
4
1 3 1 4
1
5
1 2 3 2 5
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.
题意:给一串数字,使得他们每个都不一样,需要增加几....
题解;先输入的时候算出和,然后排序,接下来循环,如果后一个等于前一个,a[i]++,如果前一个大于后一个,a[i]加上前一个减去后一个的再加上1,这样,他就不会个前面的相等.....
最后把他们的和算出来,求两个和的差就是答案...
代码如下:
1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 using namespace std; 5 int a[3005]; 6 int main() 7 { 8 int n; 9 while(scanf("%d",&n)==1) 10 { 11 int sum1=0,sum2=0; 12 for(int i=1;i<=n;i++) 13 { 14 scanf("%d",&a[i]); 15 sum1+=a[i]; 16 } 17 sort(a+1,a+n+1); 18 sum2=a[1]; 19 for(int i=2;i<=n;i++) 20 { 21 if(a[i]==a[i-1]) 22 a[i]++; 23 else if(a[i]<a[i-1]) 24 a[i]+=a[i-1]-a[i]+1; 25 sum2+=a[i]; 26 } 27 printf("%d\n",sum2-sum1); 28 } 29 return 0; 30 }