http://codeforces.com/gym/101149/problem/J
给出n个数字,表示第i条街有a[i]个照片存在过,其中,每个照片可以覆盖一段连续的区间,
就是一张照片可以覆盖[2, 5]这样,所以第[2, 5]条街出现的次数都要+1
有点贪心的思想,就是每张照片都要覆盖一段能覆盖的最长的区间(去到0为截止点)
就像1、2、3、1、2、3
这样,第一段因该是覆盖[1, 6],然后变成0、1、2、0、1、2
然后
[2, 3]
[3, 3]
[5, 6]
[6, 6]
可以观察发现第i个位置开始的个数是a[i] - a[i - 1],max(0) 修正一下。

#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL; #include <iostream> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <string> void work() { int n; cin >> n; LL ans = 0; LL last = 0; for (int i = 1; i <= n; ++i) { LL x; cin >> x; ans += max(0LL, x - last); last = x; } cout << ans << endl; } int main() { #ifdef local freopen("data.txt","r",stdin); #endif IOS; work(); return 0; }