【LG5019】[NOIP2018]道路铺设
题面
题解
(NOIP) 抄 (NOIP)差评
设当前做到了位置(i)
且(h_i) (-) (h_i)(_+)(_1) (=) (x)
若(x>0)将(x)加入贡献即可
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (ch != '-' && !isdigit(ch)) ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = data * 10 + ch - '0', ch = getchar();
return w * data;
}
#define MAX_N 100005
int N, a[MAX_N], b[MAX_N];
int main () {
N = gi();
for (int i = 1; i <= N; i++) b[i] = a[i] = gi();
for (int i = 1; i <= N; i++) b[i + 1] -= a[i];
int ans = 0;
for (int i = 1; i <= N; i++) if (b[i] > 0) ans += b[i];
printf("%d
", ans);
return 0;
}