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; 13 const int INF = 0x3f3f3f3f; 14 int a[MAXN]; 15 16 int main(void) //Codeforces Round #304 (Div. 2) B. Soldier and Badges 17 { 18 int n; 19 while (scanf ("%d", &n) == 1) 20 { 21 for (int i=1; i<=n; ++i) scanf ("%d", &a[i]); 22 sort (a+1, a+1+n); 23 24 int tot = 0; a[0] = 0; 25 for (int i=1; i<=n; ++i) 26 { 27 if (a[i-1] >= a[i]) {tot += (a[i-1] - a[i] + 1); a[i] = a[i-1] + 1;} 28 } 29 30 printf ("%d\n", tot); 31 } 32 33 return 0; 34 } 35 36 /* 37 4 38 1 3 1 4 39 5 40 1 2 3 2 5 41 4 42 4 3 2 2 43 */
时间: 2024-08-27 22:55:25