赛道修建
题面查看
其实看到题目的第一想法其实是二分,每一次找一个minx,再记录它的pos,对于(l,pos-1)和(pos+1,r)继续做,然后一看数据范围,\(1≤n≤100000\),立刻就否决了.其实在考场上说不定我就打70分了
然后我就开始对于数据进行模拟,突然灵光一闪,发现如果对于一个坑x,\(\forall depth_x+1>depth_x\),我们会将\([x,x+1]\)区间内的数全部减去\(d_x\),那么这个x就是不必要的.
于是就有了如下的贪心策略
\[ \sum_{\forall depth_x>depth_{x-1}} depth_x-depth_{x-1} \]
然后问题就迎刃而解了,附上代码
#include <iostream> using namespace std; const int maxn = 100005; long long n, a[maxn]; long long ans; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i];//a[i]即depth[i] for (int i = 2; i <= n; i++) if (a[i] > a[i - 1]) ans += a[i] - a[i - 1]; cout << ans + a[1] << endl; }
注:本题在13年考过,在USACO也有题目,等于可以拿3倍经验三个愿望一次满足