题目链接:
https://www.luogu.com.cn/problem/P1484
思路:
贪心的每次选择一个最大值种树,然后会发现如果选择了一个值,那么这个值两边的值都不能被选了。分析影响会发现,他两侧的值要么同时被选中,要么同时不被选中
例如: ……4 5 3……
当选择了5时,如果要调整成选择4的时候要么连3 一起选上,否则选择5一定是更优的选择。
因此考虑将5的影响消除,如果我们把这三个点都删掉,添加一个新的点 权值为 4+3-5,是不是就相当于没有选5而是选择了4和3.依次这样处理。将所有数放入大根堆,每次取出最大值维护,就能得到结果。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxi=1e6+7;
typedef long long ll;
struct yhh{
ll p;
int id;
bool operator<(const yhh &b) const{
//定义排序方式
return p < b.p;
}
};
int n,k,pre[maxi],nxt[maxi],del