"bzoj2006" "NOI2010" "Super Piano"

Source: Internet
Author: User

2006: [NOI2010] Super piano

Time limit:20 Sec Memory limit:552 MB
submit:1778 solved:871
[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.
Input

The first line consists of 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.
Output

There is only one integer that represents the maximum value of the melody's beauty.
Sample Input

4 3 2 3

3

2

-6

8

Sample Output

11

"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.

We deal with a thing (I,l,r) that represents the optimal value in the upper and lower bounds when the left end of the chord is I. (That is, the right endpoint in [i+l-1,i+r-1])
We throw all the best values out of the first step into a heap, and then each time we pick the largest one out of the heap, we add the ANS to this number. Suppose we select the best worthy position in this interval as where, then we split the interval [l,r] into two segments [l,where-1] and [Where+1,r], and then the two sections are put into the heap.
This is the fact that each time you select an optimal value, and then put those less than the optimal value of the number in.
The answer is after you have chosen K times.

There are two ways we can deal with the first step:
First of all we need to deal with the prefix and this thing, and then is the query interval the most value, after checking and then add I to L value is good. For the check interval maximum:
①: We can use the line segment tree. However, our number of queries is (N+K), so it will be more slow.
②: We can use ST (something like multiplication), with St can do O (1) query, will be much faster

And then the heap is directly STL, handwriting is more troublesome
(as I was weaker, I thought about how to do it all afternoon ...) )

Segment Tree +priority_queue

#include <iostream>#include <cstdio>#include <cstring>#include <queue>usingnamespace Std;#define inf 210000000LLConst intn=500100;intN,k,a[n];Long Longans=0;structs{int where, MAXN;voidInit () {maxn=-inf;where=0; }}tr[n*4];structheap{intMAXN,where, Left,llimit,rlimit;}; Priority_queueBOOL operator< (heap x,heap y) {returnX.MAXN&LT;Y.MAXN;}int inch(){intx=0, f=1;CharCh=getchar (); while(ch<' 0 '|| Ch>' 9 '){if(ch=='-') f=-1; Ch=getchar ();} while(ch>=' 0 '&&ch<=' 9 ') {x=x*Ten+ch-' 0 '; Ch=getchar ();}returnX*f;}#Define Mid (L+r)/2#define L k<<1,l,mid#define R k<<1|1,mid+1,rvoidBuildintKintLintR) {if(L==R)        {Tr[k].maxn=a[l]; TR[K].where=l;return; } build (L);    Build (R); Tr[k].maxn=max (tr[k<<1].maxn,tr[k<<1|1].MAXN);if(tr[k<<1].maxn>tr[k<<1|1].MAXN) Tr[k].where=tr[k<<1].where;if(tr[k<<1].maxn<=tr[k<<1|1].MAXN) Tr[k].where=tr[k<<1|1].where;} S Ask (intKintLintRintXintY) {S ans1,ans2; Ans1.init (); Ans2.init ();if(X&LT;=L&AMP;&AMP;Y&GT;=R)returnTR[K];if(X<=mid) Ans1=ask (l,x,y);if(Y>mid) Ans2=ask (r,x,y);if(ANS1.MAXN&LT;ANS2.MAXN)        {ANS1.MAXN=ANS2.MAXN; Ans1.where=ans2.where; }returnans1;}intMain () {intI,J,L,R,LL,RR;    S T;    Heap ans1; n=inch(); k=inch(); l=inch(); r=inch(); for(i=1; i<=n;++i) a[i]=inch(), a[i]+=a[i-1]; Build1,1, n); for(i=1; i<=n-l+1; ++i) {ll=i+l-1, Rr=min (i+r-1, n); T=ask (1,1, N,LL,RR); Q.push (heap) {t.maxn-a[i-1],t.where, I,LL,RR}); } while(k--)        {ans1=q.top ();        Q.pop (); ans+= (Long Long) ANS1.MAXN;if(ans1.where-1>=ans1.llimit) {T=ask (1,1, n,ans1.llimit,ans1.where-1); Q.push (heap) {t.maxn-a[ans1.left-1],t.where, ans1.left,ans1.llimit,ans1.where-1}); }if(ans1.where+1<=ans1.rlimit) {T=ask (1,1, n,ans1.where+1, Ans1.rlimit); Q.push (heap) {t.maxn-a[ans1.left-1],t.where, ans1.left,ans1.where+1, ans1.rlimit}); }} printf ("%lld\n", ans);}

St+priority_queue

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <cmath>using namespace Std;#define INF 210000000LLconst int N=500100;int n,k,a[n];long long ans=0;struct s{int maxn,where;} f[ -][N];struct heap{int Maxn,where,left,llimit,rlimit;}; Priority_queue<heap>Q;bool operator< (heap x,heap y) {return x.maxn<y.maxn;} int inch () { int x=0,f=1;char ch=GetChar ();  while (ch< '0' | | ch>    ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();}return x*f;}void Prepare () {int i,j;For (i=1;i<=20;++i)For (j=1;j+ (1<<i) -1<=n;++j) {F[i][j].maxn=max (f[i-1][j].maxn,f[i-1][j+ (1<<i>>1)].MAXN);if (f[i-1][j].maxn>f[i-1][j+ (1<<i>>1)].maxn) F[i][j].where=f[i-1][j].where;Else f[i][j].where=f[i-1][j+ (1<<i>>1)].where;      }}s Ask (int x,int y) {S ans1;  int t= (int) (log (y-x+1.0)/log (2.0)); Ans1.maxn=max (f[T][x].maxn,f[T][y (1<<t) +1].MAXN); if (f[T][x].maxn>f[T][y (1<<t) +1].MAXN) ans1.where=f[T][x].where; else ans1.where=f[T][y (1<<t) +1].where; return ans1;} int main () {int i,j,l,r,ll,rr;S t;heap ans1;n=in (); K=in (); L=in (); R=in ();For (i=1;i<=n;++i) {a[i]=in ();A[i]+=a[i-1];F[0][i].maxn=a[i];f[0][i].where=i;    } prepare ();For (i=1;i<=n-l+1;++i) {ll=i+l-1,rr=min (i+r-1,n);T=ask (LL,RR);Q.push (heap) {T.MAXN-A[I-1],T.WHERE,I,LL,RR});    }While (k--) {ans1=q.top ();Q.pop ();ans+= (long Long) ANS1.MAXN;if (ans1.where-1>=ans1.llimit) {T=ask (ans1.llimit,ans1.where-1);Q.push (heap) {t.maxn-a[ans1.left-1],t.where,ans1.left,ans1.llimit,ans1.where-1});        }if (ans1.where+1<=ans1.rlimit) {T=ask (ans1.where+1,ans1.rlimit);Q.push (heap) {t.maxn-a[ans1.left-1],t.where,ans1.left,ans1.where+1,ans1.rlimit});        }    }printf ("%lld\n", ans);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"bzoj2006" "NOI2010" "Super Piano"

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.