[BZOJ 1150] 数据备份
生活随笔
收集整理的這篇文章主要介紹了
[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; }?
轉載于:https://www.cnblogs.com/newera/p/9100288.html
總結
以上是生活随笔為你收集整理的[BZOJ 1150] 数据备份的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TensorFlow之tf.nn.dro
- 下一篇: Visual Studio 2017 A