51Nod 1380 Jacket Master's every three smoked one

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.