[BZOJ 1150] 数据备份

Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1150

 

Solution:

思路和洛谷P1484完全相同

只不过将求最大不相邻的点权改为最大不相邻的边权

([P1484] 种树:http://www.cnblogs.com/newera/p/8977924.html)

 

但在边界条件上还是卡了好长时间,也许一开始我就理解错了

正解应该是将a[0]=INF,保证选过a[1]后绝不选a[2],因为a[1]在不影响剩余点的位置的前提下比a[2]更优

 

我一开始认为可以由a[1]转移到a[2],于是把left[0]=1

这明显是没有必要的,但为什么会WA?以后再看吧

 

Code:

#include <bits/stdc++.h>

using namespace std;
#define F first
#define S second
typedef long long ll;
typedef pair<ll,int> P;

const int MAXN=1e5+10;
priority_queue<P,vector<P>,greater<P> > que;
int n,k,dat[MAXN],l[MAXN],r[MAXN],vis[MAXN];
ll d[MAXN],res=0;

int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&dat[i]);
    for(int i=2;i<=n;i++) 
        l[i-1]=i-2,r[i-1]=i,d[i-1]=dat[i]-dat[i-1],que.push(P(d[i-1],i-1));
    
    d[0]=d[n]=1e17;  //边界处理 
    while(k--)
    {
        while(vis[que.top().S]) que.pop();
        P t=que.top();que.pop();
        res+=t.F;vis[l[t.S]]=vis[r[t.S]]=true;
        
        d[t.S]=t.F=d[l[t.S]]+d[r[t.S]]-d[t.S];que.push(t);        
        
        l[t.S]=l[l[t.S]];r[t.S]=r[r[t.S]];
        r[l[t.S]]=t.S;l[r[t.S]]=t.S;
    }
    
    printf("%lld",res);
    return 0;
}

 

posted @ 2018-05-28 15:11  NewErA  阅读(231)  评论(0编辑  收藏  举报