Test instructions
Give some numbers, require that each number is not the same need to add a few
Ideas:
First sort, then increment each, and finally seek the sum of difference
The code is as follows:
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>Ten#include <map> One#include <list> A#include <stack> -#include <queue> -#include <iterator> the#include <vector> - - using namespacestd; - + #defineLL Long Long - #defineINF 0x3f3f3f3f + #defineMOD 1000000007 A #defineMAXN 10000010 at #defineMAXM 1000010 - - Const intMAXM =3005; - intA[MAXM]; - - //method One: in - intMain () to { + intN; - intsum; the while(SCANF ("%d", &n) = =1&&N) * { $Memset (A,0,sizeof(a));Panax Notoginseng inti; -pos =0; the for(i =0; I < n; i++ ) +scanf"%d", &a[i]); ASort (A, A +n); the for(i =1; I < n; i++ ) + { - while(A[i] <= a[i-1]) $ { $A[i] + =1; -Sum + =1; - } the } -printf"%d\n", sum);Wuyi } the - return 0; Wu } - About $ - - //Method Two: - A intMain () + { the intN; - while(SCANF ("%d", &n) = =1) $ { theMemset (A,0,sizeof(a)); the intSUM1 =0, sum2 =0; the for(inti =1; I <= N; i++ ) the { -scanf"%d", &a[i]); inSum1 + =A[i]; the } theSort (A +1, a+n+1); Aboutsum2 = a[1]; the for(inti =2; I <= N; i++ ) the { the if(A[i] = = a[i-1]) +a[i]++; - Else if(A[i] < a[i-1]) theA[i] = a[i-1] +1;BayiSum2 + =A[i]; the } theprintf"%d\n", Sum2-sum1); - } - return 0; the}
Codeforces 546b-soldier and Badges