Description
At first there is a ring, you can choose to delete an element to get his weight, while removing the adjacent two elements, the other elements to re-form the ring, asked to get the maximum value.
Sol
Heap + greed.
Start by adding all the elements from the heap, then remove an element, add the sum of the elements on both sides of the stack-the weight of the position, and delete the left and right two points.
Until you remove the elements (\frac {n} {3}\), the left and right elements can be maintained with a linked list.
This takes out an element and can then be undone, obtaining the other two weights.
XyX said that as long as the number of elements not adjacent so that he must have a legal deletion scheme.
Code
#include <cstdio> #include <utility> #include <queue> #include <algorithm> #include < iostream>using namespace std; #define MPR (b) Make_pair (A, B) typedef long LONG ll;const int N = 100005;int N,b[n],nxt[n] , Pre[n]; LL A[n]; ll ans,tmp;priority_queue<pair<ll,int> > Q;inline ll in (ll X=0,char Ch=getchar ()) {while (ch> ' 9 ' | | ch< ' 0 ') Ch=getchar (); while (ch>= ' 0 ' && ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar (); return x; }int Main () {n=in (); for (int i=1;i<=n;i++) a[i]=in (), nxt[i]=i+1,pre[i]=i-1;nxt[n]=1,pre[1]=n;for (int i=1;i<=n; i++) Q.push (MPR (A[i],i)), for (int i=1,pos,x;i*3<=n;) {x=q.top (). First,pos=q.top (). Second,q.pop (); if (x<=0) Break;if (B[pos]) continue;++i;//cout<<pos<< "" <<x<<endl;ans+=x;tmp=a[nxt[pos]]+a[pre[pos ]]-x;a[pos]=tmp;q.push (MPR (Tmp,pos)); B[nxt[pos]]=1,b[pre[pos]]=1;nxt[pre[pre[pos]]]=pos,pre[nxt[nxt[pos]]]=pos , pre[pos]=pre[pre[pos]],nxt[pos]=nxt[nxt[pos]];//pre[pos]=pre[pre[pos]],nxt[pos]=nxt[nxt[pos]],nxt[pRe[pos]]=pos,pre[nxt[pos]]=pos;} Cout<<ans<<endl;return 0;}
51Nod 1380 Jacket Master's every three smoked one