您的位置:首页 > 其它

CF Soldier and Badges (贪心)

2015-05-26 23:06 423 查看
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 = 3005;
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 = 0;i < N;i ++)
{
scanf("%d",&S[i]);
if(HAVE[S[i]])
DEAL[D_P ++] = S[i];
else
HAVE[S[i]] = true;
}

int    ans = 0;
for(int i = 0;i < D_P;i ++)
for(int j = DEAL[i] + 1;;j ++)
if(!HAVE[j])
{
ans += j - DEAL[i];
HAVE[j] = true;
break;
}
printf("%d\n",ans);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: