P4393 [BOI2007]Sequence 序列问题

P4393 [BOI2007]Sequence 序列问题

这是我做过最水的蓝题,容易发现,对于任意三个柱子,考虑两个情况。

  1. 单调增/减,答案是最高的+次高的。

  2. 高/低/次高 或 次高/低/高 那么我们坑定选择把低的和次高的合并再和高的合并。答案还是次高+最高。

所以答案就是 (displaystyle sum_{i=2}^N max(a_i, a_{i-1}))

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

typedef long long ll;
const ll MAXN = 1e6+10;

ll N, M, lst, now, ans;

int main() {
    scanf("%lld", &N); N--;
    scanf("%lld", &lst);
    while (N--) scanf("%lld", &now), ans += max(now, lst), lst = now;
    printf("%lld
", ans);
    return 0;
}
原文地址:https://www.cnblogs.com/Gensokyo-Alice/p/13904595.html