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

题目传送门

 /*
题意:问最少增加多少值使变成递增序列
贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >=
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long ll; const int MAXN = 3e3 + ;
const int INF = 0x3f3f3f3f;
int a[MAXN]; int main(void) //Codeforces Round #304 (Div. 2) B. Soldier and Badges
{
int n;
while (scanf ("%d", &n) == )
{
for (int i=; i<=n; ++i) scanf ("%d", &a[i]);
sort (a+, a++n); int tot = ; a[] = ;
for (int i=; i<=n; ++i)
{
if (a[i-] >= a[i]) {tot += (a[i-] - a[i] + ); a[i] = a[i-] + ;}
} printf ("%d\n", tot);
} return ;
} /*
4
1 3 1 4
5
1 2 3 2 5
4
4 3 2 2
*/
上一篇:Codeforces Round #622 (Div. 2) B. Different Rules(数学)


下一篇:UnicodeEncodeError: 'latin-1' codec can't encode character 解决sae flask 中文问题