2006: [NOI2010] Super piano time limit:20 Sec Memory limit:552 MB
submit:2613 solved:1297
[Submit] [Status] [Discuss] Description Small Z is a small famous pianist, recently Dr. C gave a small z a super piano, small z hope to use this piano to create the world's most beautiful music. The super piano can play n notes, numbered 1 to N. The magic of the I note is AI, where AI can be negative. A "super chord" consists of a number of consecutive notes that contain no less than L and no more than R. We define the beauty of the Super chord for the sum of the wonderful degrees of all the notes it contains. Two super chords are thought to be the same, when and only if these two super chords contain a set of notes that are the same. Small Z decided to create a song composed of K super-chords, in order to make the music more beautiful, small z requires that the song is composed of K different super chords. We define the beauty of a piece of music as the sum of the wonderful degrees of all the super chords it contains. Little Z wants to know the maximum amount of music he can create. The first line of input contains four positive integers n, K, L, R. where n is the number of notes, K is the number of super chords contained in the song, and L and R are the lower and upper limits of the number of notes contained in the Super chord. Next n rows, each line contains an integer AI, which represents the degree of beauty of each note by number from small to large. N<=500,000k<=500,000-1000<=ai<=1000,1<=l<=r<=n and guaranteed that there must be a music output that satisfies the conditions
There is only one integer that represents the maximum value of the melody's beauty.
Sample Input4 3 2 3
3
2
-6
8Sample Output11
"Sample description"
There are 5 different kinds of super chords:
Note 1 ~ 2, the degree of beauty is 3 + 2 = 5
Note 2 ~ 3, 2 + (-6) = 4
Note 3 ~ 4, the degree of beauty is (-6) + 8 = 2
Notes 1 ~ 3, 3 + 2 + (-6) = 1
Note 2 ~ 4, 2 + (-6) + 8 = 4
The best scenario is that the music consists of chord 1, chord 3, and chord 5, with a wonderful degree of 5 + 2 + 4 = 11. HINT
Source [Submit] [Status] [Discuss]
The study of the puzzle, reproduced in the next--
Heap +rmq
Defines a four-tuple (i,l,r,t) representation that begins with the first, ending at l-r (satisfies the length >=l,<=r), and ends at the weight and maximum of T.
First, the four-tuple that starts with each bit is added to the heap, when the l,r is exactly the length >=L,<=R;
The keywords that are sorted in the heap are the sum of weights.
Then take the heap top out (i,l,r,t), and then add (I,l,t-1,t ') and (I,t+1,r,t ') to the heap (because you want to meet any of the two different conditions of a sequence)
So how does t in four-tuple quickly find out?
With RMQ: To maintain the prefix and, the weight value and is sum[t]-sum[i-1], for a four-tuple sum[i-1] the same, so long as found L-r Sum[i] The largest can be.
1#include <queue>2#include <cstdio>3 4InlineintNextchar (void)5 {6 Const Static intSiz =1024x768;7 8 Static CharBuf[siz];9 Static Char*HD = buf +siz;Ten Static Char*TL = buf +siz; One A if(HD = =TL) -Fread (HD = buf,1, Siz, stdin); - the return*hd++; - } - -InlineintNextint (void) + { -RegisterintRET =0; +RegisterintNeg =false; ARegisterintbit =Nextchar (); at - for(; bit < -; bit =Nextchar ()) - if(bit = ='-') Neg ^=true; - - for(; bit > -; bit =Nextchar ()) -RET = RET *Ten+ Bit- -; in - returnNeg? -Ret:ret; to } + - Const intSiz =500005; the * intN, M, L, R; $ Panax Notoginseng intNum[siz]; - intSum[siz]; the + intst[siz][ -], Log[siz]; A theInlinevoidPREWORKRMQ (void) + { - for(inti =1; I <= N; ++i) $st[i][0] =i; $ - for(inti =1; (1<< i) <= N; ++i) - for(intj =1; J + (1<< i)-1<= N; ++j) the { - intx = St[j][i-1];Wuyi inty = st[j + (1<< (I-1))][i-1]; theSt[j][i] = sum[x] > sum[y]?x:y; - } Wu -log[0] = -1; About $ for(inti =1; I <= N; ++i) -Log[i] = Log[i >>1] +1; - } - AInlineintQueryintLintR) + { the if(L = =R) - returnl; $ the intt = log[r-l +1]; the the intx =St[l][t]; the inty = st[r-(1<< T) +1][t]; - in returnSUM[X] > Sum[y]?x:y; the } the About structData the { the intp, T, L, R; the +Datavoid) {}; -DataintAintBintCintd): the P (a), T (b), L (c), R (d) {};Bayi }; the theInlineBOOL operator< (ConstData &a,ConstData &b) - { - returnSUM[A.T]-SUM[A.P-1] < SUM[B.T]-SUM[B.P-1]; the } the thestd::p riority_queue<data>h; the -Signed Main (void) the { theN =nextint (); theM =nextint ();94L =nextint (); theR =nextint (); the the for(inti =1; I <= N; ++i)98Num[i] =nextint (); About - for(inti =1; I <= N; ++i)101Sum[i] = sum[i-1] +Num[i];102 103 preworkrmq ();104 the Long LongAns =0;106 107 for(inti =1; I <= N-l +1; ++i)108 {109 intL = i + L-1; the intR = i + R-1;111 the if(R >N)113R =N; the the H.push (data (I, Query (L, R), L, R)); the }117 118 for(inti =1; I <= M; ++i)119 { -Data top =h.top (); H.pop ();121 122Ans + = sum[top.t]-SUM[TOP.P-1];123 124 if(Top.t >TOP.L) theH.push (data (TOP.P, query (TOP.L, TOP.T-1), TOP.L, top.t-1));126 if(TOP.T <TOP.R)127H.push (data (TOP.P, query (top.t +1, TOP.R), top.t +1, TOP.R)); - }129 theprintf"%lld\n", ans);131}
@Author: Yousiki
Bzoj 2006: [NOI2010] Super piano