首页 > 代码库 > Week 1 # O Soldier and Badges
Week 1 # O Soldier and Badges
原题描述:
O - 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
4
1 3 1 4
1
5
1 2 3 2 5
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 by1.
这题我开始我没认真看,以为只是不能有一样的数字,错了好几次都算不出来,后来看了一下题目,不仅不能相同数字,当数字相同时,只能加不能减。
题目很简单,也就只用了一个sort排序。看看代码都能懂。
AC代码:
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 int main() 5 { 6 int n,s=0,k=0; 7 cin>>n; 8 int a[n+1]; 9 a[0]=0; 10 for(int i=1;i<=n;i++) 11 cin>>a[i]; 12 sort(a+1,a+n+1); 13 for(int i=1;i<=n;i++) 14 { 15 if(a[i]==i+k) continue; 16 else if(a[i]<i+k) s+=i+k-a[i]; 17 else k+=a[i]-i-k; 18 } 19 cout<<s<<endl; 20 return 0; 21 }
Week 1 # O Soldier and Badges