[APIO/CTSC 2007] 数据备份
作者:互联网
教会了我反悔DP。
考虑一步一步来,先思考假如只需要一条路怎么办,很明显直接选最小的;但两条路呢?只有两种方案,要么再选次小的,要么选最小两端的元素(不可能只选一端,如果那样最小就不是最小了)。为了使得后续可以反悔每次考虑pop最小点之后push一个新点,点权为两端点权和减去自身点权,删去两端节点即可。用双向链表维护。
#include<bits/stdc++.h>
//#define zczc
#define int long long
const int N=100010;
const int maxn=1e12;
using namespace std;
inline void read(int &wh){
wh=0;int f=1;char w=getchar();
while(w<'0'||w>'9'){if(w=='-')f=-1;w=getchar();}
while(w<='9'&&w>='0'){wh=wh*10+w-'0';w=getchar();}
wh*=f;return;
}
struct node{
int wh,val;
};
inline bool operator <(node s1,node s2){
return s2.val<s1.val;
}
priority_queue<node>q;
int m,n,a[N],l[N],r[N];
bool del[N];
inline void delet(int wh){
del[wh]=true;
r[l[wh]]=r[wh];
l[r[wh]]=l[wh];
}
signed main(){
#ifdef zczc
freopen("in.txt","r",stdin);
#endif
read(m);read(n);m--;
int sum,in;read(in);sum=in;
q.push((node){0,maxn});
q.push((node){m+1,maxn});
r[0]=1,l[m+1]=m;a[0]=a[m+1]=maxn;
for(int i=1;i<=m;i++){
l[i]=i-1,r[i]=i+1;
read(in);a[i]=in-sum;sum=in;
q.push((node){i,a[i]});
}
int ans=0;
for(int i=1;i<=n;i++){
node now=q.top();q.pop();
if(del[now.wh]){
i--;continue;
}
ans+=now.val;
//printf("now:%d %d\n",now.wh,now.val);
int ll=l[now.wh],rr=r[now.wh];
a[now.wh]=a[ll]+a[rr]-a[now.wh];
delet(ll);delet(rr);
q.push((node){now.wh,a[now.wh]});
}
printf("%lld",ans);
return 0;
}
标签:node,int,CTSC,wh,read,maxn,2007,APIO,getchar 来源: https://www.cnblogs.com/dai-se-can-tian/p/16417558.html