题意大概是 让你种树 m棵树 n个位置 相邻的两个位置不能种树 种树需要钱 问 需要的最少的钱 要多少 首先考虑 dp 我实在是不会 看了首题解 原来是带悔贪心 是我没学过的东西= 然后 主要思路 就是 我先选择了一个 位置 是最大的 列如 ak 那么 ak - 1 和 ak + 1 都不能选 但是 如果 ak - 1 + ak + 1 比 ak大的话 那么肯定是选择 ak - 1 + ak + 1 那么这个时候 我们先选择 ak 然后 将 ak - 1 + ak + 1 - ak 存入链表 将ak 和 ak - 1 和 ak + 1 删掉 此时 若选择 ak - 1 + ak + 1 - ak的 话 那么值将变为 ak - 1 + ak + 1 这就是可以反悔的贪心 芜湖~
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<utility>
#define x first
#define y second
#define endl "\n"
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
typedef pair<int,int> PII;
int n,m;
int a[N];
int flag[N];
ll to[N],last[N],w[N];
void addl(int x,int y,ll z){
to[last[y]] = x;
last[x] = last[y];
to[x] = y;
w[x] = z;
last[y] = x;
}
void addr(int x,int y,ll z){
last[to[y]] = x;
to[x] = to[y];
to[y] = x;
w[x] = z;
last[x] = y;
}
void del(int x){
to[last[x]] = to[x];
last[to[x]] = last[x];
flag[x] = 1;
to[x] = 0;
w[x] = 0;
last[x] = 0;
}
priority_queue<PII>q;
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
scanf("%d",&a[i]);
}
if(m * 2 > n){
cout << "Error!" <<endl;
return 0;
}
for(int i = 1; i <= n; i++){
addr(i,i - 1,a[i]);
q.push({a[i],i});
}
last[1] = n;
to[n] = 1;
int sum = 0;
for(int i = 1; i <= m; i++){
while(flag[q.top().y]) q.pop();
PII p = q.top();
q.pop();
//cout << p.x << " " << p.y << endl;
sum += p.x ;
w[p.y] = w[last[p.y]] + w[to[p.y]] - w[p.y];
del(last[p.y]);
del(to[p.y]);
q.push({w[p.y],p.y});
}
cout << sum << endl;
return 0;
}