CF Soldier and Badges (贪心)

Soldier and Badges
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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.

Sample test(s)
input
4
1 3 1 4
output
1
input
5
1 2 3 2 5
output
2

可以得出一个简单的结论,如果有相同的几个出现,那么重复这几个一定要改变,而且只能增大,所以把每一个增大到距离它最近的没有用过那个位置。至于为什么这样的贪心策略是对的。。。。我也不知道,感觉罢了。
 #include <iostream>
#include <fstream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; const int SIZE = ;
bool HAVE[SIZE + SIZE];
int N,D_P;
int S[SIZE],DEAL[SIZE];
vector<int> CHOOSE[SIZE]; int main(void)
{
scanf("%d",&N);
for(int i = ;i < N;i ++)
{
scanf("%d",&S[i]);
if(HAVE[S[i]])
DEAL[D_P ++] = S[i];
else
HAVE[S[i]] = true;
} int ans = ;
for(int i = ;i < D_P;i ++)
for(int j = DEAL[i] + ;;j ++)
if(!HAVE[j])
{
ans += j - DEAL[i];
HAVE[j] = true;
break;
}
printf("%d\n",ans); return ;
}
上一篇:【高可用HA】Apache (1) —— Mac下安装Apache Httpd到自定义路径(非/etc/apache2)


下一篇:一,入门基础—— 2. 第一个project项目