2006: [noi2010] Super piano-bzoj

Source: Internet
Author: User

Description
Little Z is a famous pianist. Recently, Dr. C gave little Z a super piano. Little Z hopes to use it to create the most wonderful music in the world. This super piano can play n notes numbered 1 to n. The beauty of the I-th note is Ai, where AI can be positive or negative. A "superchord" is composed of several consecutive numbered notes, containing no less than l and no more than R. We define the beauty of a super chord as the sum of the beauty of all its notes. Two superchords are considered to be the same if and only if the set of notes contained by these two superchords is the same. Little Z decided to create a piece of music consisting of K super chords. To make the music more appealing, little Z asked the piece to be composed of k different super chords. We define the beauty of a piece of music as the sum of the beauty of all its super chords. Z wants to know the maximum value of the music he can create.
Input
The first line contains four positive integers n, k, l, and R. Where N is the number of notes, k is the number of super chords contained in the music, and l and R are the lower limit and upper limit of the number of notes contained in the super chord, respectively. In the next n rows, each line contains an integer AI, indicating the beauty of each note from small to large by number.
Output
There is only one integer, indicating the maximum music beauty.
Sample Input
4 3 2 3

3

2

-6

8

Sample output
11

[Example]
There are 5 different super chords:

Note 1 ~ 2, the beauty is 3 + 2 = 5
Note 2 ~ 3, the beauty is 2 + (-6) =-4
Note 3 ~ 4, The beauty is (-6) + 8 = 2
Note 1 ~ 3, the beauty is 3 + 2 + (-6) =-1
Note 2 ~ 4, The beauty is 2 + (-6) + 8 = 4
The optimal solution is: Music consists of chord 1, Chord 3, and chord 5. The beauty is 5 + 2 + 4 = 11.

 

I got down to jzp, but I can't think of it.

First, there was a question like this: there are two ordered arrays A and B. C [I, j] indicates a [I] + B [I], and the first K of the output is small.

We all know that we use the heap to maintain the current minimum value of each row (that is, the number at the top). Every time we get an element, we will add the column of this row to the heap.

At first, I thought about it. First, I had to deal with a prefix and interval [L, the sum of R] becomes s [R]-L-1], so it also becomes something similar to the one above, but it is out of order.

So we also need to support the search range K, which is slightly more complex.

Therefore, jzp provides a better result. We use triple (I, L, R) to indicate that the Left endpoint is I, and the right endpoint is then the maximum value of [L, R, apparently there are n elements at the beginning

Then, when we take out the element (I, L, R), we need to update it. We split it into two parts. If the maximum value is obtained at K, we will (I, l, k-1) and (I, K + 1, R) join in heap

In this way, the complexity is O (n + k) log (n + k), and there is no pressure.

  1 const  2     maxn=500500;  3 type  4     node=record  5         i,l,r,max:longint;  6     end;  7 var  8     q:array[0..maxn*2]of node;  9     s:array[0..maxn]of int64; 10     f:array[0..maxn,0..20]of longint; 11     n,k,l,r,tot:longint; 12     ans:int64; 13   14 procedure rmq; 15 var 16     i,k:longint; 17 begin 18     for i:=1 to n do f[i,0]:=i; 19     k:=0; 20     while 1<<k<<1<=n do 21         begin 22             for i:=1 to n-1<<k<<1+1 do 23                 if s[f[i,k]]>s[f[i+1<<k,k]] then f[i,k+1]:=f[i,k] 24                 else f[i,k+1]:=f[i+1<<k,k]; 25             inc(k); 26         end; 27 end; 28   29 function max(l,r:longint):longint; 30 var 31     k:longint; 32 begin 33     k:=0; 34     while r-l+1>1<<k<<1 do inc(k); 35     if s[f[r-1<<k+1,k]]>s[f[l,k]] then exit(f[r-1<<k+1,k]); 36     exit(f[l,k]); 37 end; 38   39 procedure swap(var x,y:node); 40 var 41     t:node; 42 begin 43     t:=x;x:=y;y:=t; 44 end; 45   46 procedure up(x:longint); 47 var 48     i:longint; 49 begin 50     while x>1 do 51         begin 52             i:=x>>1; 53             if s[q[x].max]-s[q[x].i]> s[q[i].max]-s[q[i].i] then 54                 begin 55                     swap(q[i],q[x]); 56                     x:=i; 57                 end 58             else exit; 59         end; 60 end; 61   62 procedure down(x:longint); 63 var 64     i:longint; 65 begin 66     i:=x<<1; 67     while i<=tot do 68         begin 69             if (i<tot) and (s[q[i+1].max]-s[q[i+1].i]>s[q[i].max]-s[q[i].i]) then inc(i); 70             if s[q[i].max]-s[q[i].i]>s[q[x].max]-s[q[x].i] then 71                 begin 72                     swap(q[i],q[x]); 73                     x:=i;i:=x<<1; 74                 end 75             else exit; 76         end; 77 end; 78   79 procedure insert(i,l,r:longint); 80 begin 81     if r>n then r:=n; 82     if l>r then exit; 83     inc(tot); 84     q[tot].i:=i;q[tot].l:=l;q[tot].r:=r; 85     q[tot].max:=max(l,r); 86     up(tot); 87 end; 88   89 procedure delete; 90 begin 91     swap(q[1],q[tot]); 92     dec(tot); 93     down(1); 94 end; 95   96 procedure main; 97 var 98     i:longint; 99 begin100     read(n,k,l,r);101     for i:=1 to n do read(s[i]);102     for i:=2 to n do inc(s[i],s[i-1]);103     rmq;104     for i:=0 to n-1 do insert(i,i+l,i+r);105     for i:=1 to k do106         begin107             inc(ans,s[q[1].max]-s[q[1].i]);108             insert(q[1].i,q[1].l,q[1].max-1);insert(q[1].i,q[1].max+1,q[1].r);109             delete;110         end;111     writeln(ans);112 end;113  114 begin115     main;116 end.
View code

 

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.