CodeForces 546B-Soldier and Badges

题意:
  给出一些数字,要求每个数字都不一样需要增加几

思路:
  先排序,然后一个个增加,最后求总和差

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <fstream>
 5 #include <cmath>
 6 #include <ctime>
 7 #include <cstdlib>
 8 #include <algorithm>
 9 #include <set>
10 #include <map>
11 #include <list>
12 #include <stack>
13 #include <queue>
14 #include <iterator>
15 #include <vector>
16
17 using namespace std;
18
19 #define LL long long
20 #define INF 0x3f3f3f3f
21 #define MOD 1000000007
22 #define MAXN 10000010
23 #define MAXM 1000010
24
25 const int maxm = 3005;
26 int a[maxm];
27
28 //方法一:
29
30 int main()
31 {
32     int n;
33     int sum;
34     while(scanf("%d", &n)==1&&n)
35     {
36         memset(a, 0, sizeof(a));
37         int i;
38         pos = 0;
39         for(i = 0; i < n; i++ )
40             scanf("%d", &a[i]);
41         sort(a, a+n);
42         for(i = 1; i < n; i++ )
43         {
44             while(a[i] <= a[i-1])
45             {
46                 a[i] += 1;
47                 sum += 1;
48             }
49         }
50         printf("%d\n", sum);
51     }
52
53     return 0;
54 }
55
56
57
58
59 //方法二:
60
61 int main()
62 {
63     int n;
64     while(scanf("%d", &n) == 1)
65     {
66         memset(a, 0, sizeof(a));
67         int sum1 = 0, sum2 = 0;
68         for(int i = 1; i <= n; i++ )
69         {
70             scanf("%d", &a[i]);
71             sum1 += a[i];
72         }
73         sort(a+1, a+n+1);
74         sum2 = a[1];
75         for(int i = 2; i <= n; i++ )
76         {
77             if(a[i] == a[i-1])
78                 a[i]++;
79             else if(a[i] < a[i-1])
80                 a[i] = a[i-1] + 1;
81             sum2 += a[i];
82         }
83         printf("%d\n", sum2 - sum1);
84     }
85     return 0;
86 }
时间: 2024-12-29 07:46:33

CodeForces 546B-Soldier and Badges的相关文章

CF 546B Soldier and Badges

Describe 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 soldi

546B. Soldier and Badges

题目链接 题意: n个数,要保证这n个数完全不相同,求需要把原来的数增加多少,求这个值得最小值   Java 程序 import java.io.PrintStream; import java.util.Arrays; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class B546 { static void run(){ Scanner in = new Scanner(S

贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges

题目传送门 1 /* 2 题意:问最少增加多少值使变成递增序列 3 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 typedef long long ll; 11 12 const int MAXN = 3e3 + 10;

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

Codeforces Round #304 (Div. 2)——1002—— 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 o

CodeForces 546B

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 o

第一次比赛的 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 so

Soldier and Badges (set的检索简单运用)

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 o

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 o

Codeforces 546D Soldier and Number Game(数论)

类似筛素数的方法--求出前缀和.然后直接O(1)回答即可. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define rep(i,a,b) for(int i(a); i <= (b); ++i) 6 7 const int N = 10000000 + 10; 8 9 int num[N]; 10 bool p[N]; 11 int T, n, m; 12 13 int main(){ 14 15 memset(p,