Use heap maintenance doubly linked list to greedy ...
The data range obviously does not allow O (nm) of the dp>_<. And the DP light state is n*m. Obviously can't optimize.
Probably think of greedy mess ... Initially want to greedy through a few small negative numbers to connect a positive number into a paragraph, but in the end is to join or directly throw away bad judgment
Then I ran to the puzzle ... The solving is very good, even I have read >_<. Address: http://www.cnblogs.com/tuigou/p/4868127.html
Although the choice of positive and negative values is different, the actual operation is to combine the numbers of the two sides together. Also, for negative numbers at the left or right end, deleting them will not reduce the number of segments currently selected.
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5#include <queue>6#include <cstdlib>7#include <cmath>8 #definell Long Long9 using namespacestd;Ten Const intmaxn=100233; One structzs{ A intID; - }; -Priority_queue <zs>Q; the inta[maxn],cnt,pre[maxn<<1],next[maxn<<1],v[maxn<<1]; - intI,j,n,m,zsnum,ans; - BOOLdel[maxn<<1]; - + intRA,FH;CharRx; -InlineintRead () { +Rx=getchar (), ra=0, fh=1; A while((rx<'0'|| Rx>'9') &&rx!='-') rx=GetChar (); at if(rx=='-') fh=-1, rx=GetChar (); - while(rx>='0'&&rx<='9') ra*=Ten, ra+=rx- -, Rx=getchar ();returnTalfh; - } - BOOL operator< (ZS A,zs b) {returnABS (V[a.id]) >ABS (V[b.id]);} - intMain () { -N=read (), M=read ();if(!m) {Puts ("0");return 0;} in for(i=1; i<=n;i++) A[i]=read (); cnt=0; - for(i=1; i<=n;v[cnt]+=a[i++]) to if(LL) a[i]* (LL) v[cnt]<0||! CNT) cnt++; + for(i=1; i<=cnt;i++)if(v[i]>0) zsnum++,ans+=V[i]; - if(zsnum>m) { the for(i=1; i<=cnt;i++) Q.push ((ZS) {i}), pre[i]=i-1, next[i]=i+1;//, printf ("%d", V[i]);p UTS (""); *pre[1]=next[cnt]=0; $ for(i=zsnum-m;i;i--){Panax Notoginseng while(!q.empty () &&del[q.top (). Id]) Q.pop ();if(Q.empty ()) Break; - intX=q.top (). id,pr=pre[x],nex=Next[x]; the +Q.pop (), Ans-=abs (V[x]), del[x]=1; A if(! (pr&&NEX)) { the if(v[x]<0) i++,ans+=ABS (V[x]); + if(PR) next[pr]=0;if(NEX) pre[nex]=0; - } $ Else{ $del[pr]=del[nex]=1; -v[++cnt]=v[pr]+v[x]+V[nex]; - Q.push (ZS) {cnt}); the if(PRE[PR]) pre[cnt]=pre[pr],next[pre[cnt]]=CNT; - if(Next[nex]) next[cnt]=next[nex],pre[next[cnt]]=CNT;Wuyi } the } - } Wuprintf"%d\n", ans); - return 0; About}
View Code
[bzoj2288] [POJ Challenge] Birthday present