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
output
input
output
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; }
相关文章推荐
- 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
- CF 546 B. Soldier and Badges
- cf#304-B. Soldier and Badges-暴力贪心
- Codeforces #304 (Div. 2) B. Soldier and Badges ( 贪心
- coderforce 546 B. Soldier and Badges
- Codeforces Round #304 (Div. 2) B. Soldier and Badges 水题
- CF - 766B. Mahmoud and a Triangle - 排序+贪心
- CF 518 E Arthur and Questions(贪心,模拟乱搞)
- cf#AIM Tech Round -C. Graph and String-贪心/ 二分图染色
- CF - 389 - B. Fox and Cross(贪心)
- codeforces546-Soldier and Badges(模拟)
- CF 262 C. Maxim and Discounts 贪心(水)
- CodeForces 546B - Soldier and Badges(模拟)
- 【codeforces 546B】Soldier and Badges
- CF 557A(Ilya and Diplomas-贪心)
- CF 508C(Anya and Ghosts-贪心)
- CF 518E(Arthur and Questions-贪心)
- CodeForces-546B-Soldier and Badges
- CF 628C --- Bear and String Distance --- 简单贪心
- CF 525C(Ilya and Sticks-贪心)