这题需要一步转化,就变成和 BZOJ1150: [CTSC2007]数据备份Backup 一样了。但不太能直接想到。
为什么题意就等价于选一些不相邻的点呢?
n个点的环,我们选中n/3个点,一定存在一个选中的点两侧有连续两个不选的的点,则我们把这个要选的点取掉,不会有两个要选的点变成相邻。
剩下的就是贪心啦。
#include<cstdio>
#include<queue>
#include<algorithm>
#define Fir first
#define Sec second
using namespace std;
const int maxn=200005;
typedef long long LL;
typedef pair<LL,int> Pair;
int n,L[maxn],R[maxn];
LL w[maxn],ans;
priority_queue< Pair > _heap;
bool vis[maxn];
int main(){
freopen("51nod1380.in","r",stdin);
freopen("51nod1380.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&w[i]);
for(int i=1;i<=n;i++) L[i]=i-1, R[i]=i+1, _heap.push(make_pair(w[i],i));
L[1]=n; R[n]=1;
for(int i=1;i<=n/3;i++){
Pair x=_heap.top(); while(vis[x.Sec]) _heap.pop(), x=_heap.top();
_heap.pop(); ans+=x.Fir;
int tL=L[x.Sec], tR=R[x.Sec]; vis[tL]=vis[tR]=true;
L[x.Sec]=L[tL]; R[x.Sec]=R[tR]; R[L[x.Sec]]=L[R[x.Sec]]=x.Sec;
w[x.Sec]=w[tL]+w[tR]-w[x.Sec]; _heap.push(make_pair(w[x.Sec],x.Sec));
}
printf("%lld\n",ans);
}