Codeforces 957D 思维
D. Riverside Curio
题意:
有 n 天,每天会在水位线上画一条线,告诉你每天在水位线上方已经有 a[i] 条线。问你 n 天在水位线下方的线总和最少是几条?
tags:
考虑 f[i] 为第 i 天的线条数量,相邻的两天 f[] 最多相差 1 。
则 f[i] >= a[i]+1, f[i] >= f[i-1], f[i] >= f[i+1]-1 。
// 957D
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define rep(i,a,b) for (int i=a; i<=b; ++i)
#define per(i,b,a) for (int i=b; i>=a; --i)
#define mes(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define MP make_pair
#define PB push_back
#define fi first
#define se second
typedef long long ll;
const int N = 200005;
int n;
ll a[N], f[N], ans;
int main()
{
scanf("%d", &n);
rep(i,1,n) {
scanf("%lld", &a[i]);
f[i] = max(f[i-1], a[i]+1);
}
per(i,n,1) {
f[i] = max(f[i+1]-1, f[i]);
ans += f[i]-a[i]-1;
}
printf("%lld\n", ans);
return 0;
}