CodeForces 546B-Soldier and Badges
Posted x心有灵犀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }
以上是关于CodeForces 546B-Soldier and Badges的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #546 (Div. 2)
Nastya Hasn't Written a Legend(Codeforces Round #546 (Div. 2)E+线段树)
Codeforces Round #546 (Div. 2) D. Nastya Is Buying Lunch