HDU3415: Max Sum of Max-K-sub-sequence (monotonous Queue)

Source: Internet
Author: User

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

 

This is a question of our training competition. The speaker said "What Is DP"? Sorry, after the competition, I can't do it in monotonous queue.

Then I took a decisive look at the monotonous queue, which was written only after referring to others' code.

 

A monotonous queue is a queue that keeps the elements in the queue increasing or decreasing monotonically. It can be deleted from both ends and can only be inserted from the end of the queue. The specific function of a monotonic queue is to ensure that the elements in the queue meet the monotonicity. For each j in the preceding problem, the corresponding s [I] can be found at O (1) time. (If the elements in the queue increase monotonically, the first element is the desired element ).

Maintenance Method: for every j, we insert s [J-1] (why not s [j]? The subscript at the beginning of the interval is maintained in the queue, and the subscript at the end of the interval is j. To ensure the monotonicity of the queue, we can delete elements from the end of the queue until the end of the queue is better than the elements to be inserted. (In this question, the value is smaller than the element to be inserted, the location is higher than that of the element to be inserted, But this condition can be left blank.) The current element is inserted at the end of the team. The reason why we can delete all the elements at the end of the previous queue is that they are no longer the most optimal elements, because the elements to be inserted are located at a higher position and their values are smaller than those at the same time. We are looking for the smallest s [I] In I that satisfies (I> = j-k + 1), the larger the location, the more likely it will become the optimal s [I] of j.

After an element is inserted, all the elements that do not meet the constraints (I> = j-k + 1) are deleted from the beginning of the queue. At this time, the queue must not be empty. (Because a qualified element has just been inserted)

 

 

# Include <stdio. h> # include <queue> # include <algorithm> # include <string. h> using namespace std; int a [111111]; int sum [211111]; const int INF = 0x3fffffff; int main () {int t, n, m, I, j, k, head, end; scanf ("% d", & t); while (t --) {scanf ("% d", & n, & k ); j = n; sum [0] = 0; for (I = 1; I <= n; I ++) {scanf ("% d ", & a [I]); sum [I] = sum [I-1] + a [I]; // Save the preceding I and all to the sum array} int ans =-INF; for (I = n + 1; I <n + k; I ++) sum [I] = sum [I-1] + A [I-n]; n = n + k-1; deque <int> Q; // bidirectional queue Q. clear (); for (I = 1; I <= n; I ++) {while (! Q. empty () & sum [I-1] <sum [Q. back ()]) // keep the monotonicity of the queue Q. pop_back (); while (! Q. empty () & Q. front () <I-k) // If the length exceeds k, the Q. pop_front (); Q. push_back (I-1); if (sum [I]-sum [Q. front ()]> ans) // record, sum [n]-sum [m] indicates the sum between N-1 and m + 1 and {ans = sum [I]-sum [Q. front ()]; head = Q. front () + 1; end = I ;}if (end> j) end % = j; printf ("% d \ n", ans, head, end) ;}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.