Hdu3415 Max Sum of Max-K-sub-sequence)

Source: Internet
Author: User

Max Sum of Max-K-sub-sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 4836 Accepted Submission (s): 1765


Problem Description
Given a circle sequence A [1], A [2], A [3] ...... A [n]. circle sequence means the left neighbor of A [1] is A [n], and the right neighbor of A [n] is A [1].
Now your job is to calculate the max sum of a Max-K-sub-sequence. max-K-sub-sequence means a continuous non-empty sub-sequence which length not exceed K.
 

Input
The first line of the input contains an integer T (1 <= T <= 100) which means the number of test cases.
Then T lines follow, each line starts with two integers N, K (1 <= N <= 100000, 1 <= K <= N ), then N integers followed (all the integers are between-1000 and 1000 ).
 

Output
For each test case, you shoshould output a line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. if there are more than one result, output the minimum start position, if still more than one, output the minimum length of them.
 

Sample Input
4
6 3
6-1 2-6 5-5
6 4
6-1 2-6 5-5
6 3
-1 2-6 5-5 6
6 6
-1-1-1-1-1-1-1

Sample Output
7 1 3
7 1 3
7 6 2
-1 1 1
Obtain the maximum value, start position, and end position when the sum of n consecutive numbers cannot exceed m (minus n if n is greater than) in the n number of composition rings)
I think it is still necessary to do this. Although it is a bit like the monotonous queue question previously done, I did this for the first time.

# Include <stdio. h> int a [100005], sum [200005], q [200005]; int main () {int t, n, m, I, j, maxx, e, s, first, last; scanf ("% d", & t); while (t --) {scanf ("% d", & n, & m ); sum [0] = 0; for (I = 1; I <= n; I ++) {scanf ("% d", & a [I]); sum [I] = sum [I-1] + a [I];} for (; I <= n + m; I ++) sum [I] = sum [I-1] + a [I-n]; // extend m backward to achieve the effect of loop first = last = 0; q [last ++] = 0; // this should be noted that WA has been used many times and does not have enough understanding of monotonous queues. maxx = a [1]; e = s = 1; for (I = 1; I <= n + m; I ++) {while (first <last & sum [Q [last-1]> sum [I-1]) // find the minimum value? Because sum [I]-sum [q [first], sum [I] is a fixed value // so to maximize this item, we must minimize sum [q [first] within the range of m {last --;} q [last ++] = I-1; // record is also I-1 while (first <last & q [first] <I-m) // remove first ++ out of range; if (maxx <sum [I]-sum [q [first]) {s = q [first] + 1 ;//, we recorded the I-1 at that time, here to calculate the starting point, we need to add 1 e = I; // The end point maxx = sum [I]-sum [q [first]; // maximum value} if (s> n) // do not drop s-= n; if (e> n) e-= n; printf ("% d \ n", maxx, s, e);} return 0 ;}

 

Related Article

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.