HDU 3415 Max Sum of max-k-sub-sequence (monotone queue)

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/u012860063

Max Sum of Max-k-sub-sequenceTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 5791 Accepted Submission (s): 2083


Problem Descriptiongiven a circle sequence a[1],a[2],a[3] ... A[n]. Circle sequence means the left neighbour of A[1] was a[n], and the right neighbour of A[n] is a[1].
Now your job was 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.
Inputthe 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 the integers n, K (1<=n<=100000, 1<=k<=n), then N integers followe D (all the integers is between-1000 and 1000).
Outputfor, should output a line contains three integers, the Max Sum in the sequence, the start Positio N of the sub-sequence, the end position of the sub-sequence. If there is more than one result, output the minimum start position, if still more than one, output the minimum length O F them.
Sample Input
46 36-1 2-6 5-56 46-1 2-6 5-56 3-1 2-6 5-5 66 6-1-1-1-1-1-1

Sample Output
7 1 37 1 37 6 2-1 1 1


Attach others ' ideas:

A monotone queue is a queue that keeps elements in the queue monotonically increasing (or decreasing), and can be removed from both ends. Can only be inserted from the end of the team. The detailed function of the monotone queue is. Because the elements in the hold queue satisfy monotonicity, for each j of the above problem, it is possible to find the corresponding s[i at O (1) time]. (Keeping the elements in the queue monotonically increasing, the first element of the team is the desired element).

Maintenance method: For each J, we insert s[j-1] (why not S[J]?

The queue maintains the subscript for the start of the interval, and J is the subscript for the end of the interval. Inserted from the end of the team. To ensure the monotony of the queue, we remove the element from the tail until the tail element is superior to the element that is currently required (the value in the subject is smaller than the element to be inserted). The position is more than the element to be inserted before. Just after this condition is not considered), the current element is inserted at the end of the queue. It is possible to delete all the trailing elements of the previous queue because they have not been able to be the optimal element. The values are smaller than the elements that are currently being inserted before they are positioned above them. What we are looking for is to satisfy (i>=j-k+1) the smallest s[i in the i], the larger the position the more likely to become the best s[i of J in the back].

After inserting an element, all elements that do not meet the constraint (i>=j-k+1) are removed from the beginning of the team, and the queue must not be empty at this time.

(because a certain qualifying element has just been inserted)

Code such as the following: (see someone else's to do it, Khan)

#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #include < iostream>using namespace std; #define INF 0x3fffffffint Sum[100047],a[200047];int main () {int t,i,n,m,k,head,end; scanf ("%d", &t), while (t--) {memset (sum,0,sizeof (sum)), scanf ("%d%d", &n,&k), for (i = 1; I <= n; i++) {scanf ("%d", &a[i]); Sum[i] = sum[i-1]+a[i];//The preceding I and all in the sum array}for (i = n+1; i < n+k; i++) {Sum[i] = sum[i-1]+a[i-n];//will be before n+k -1 items and all deposit in sum array}int ans =-inf;//initialize ans for minimum deque<int>q; Q.clear ();//empty bidirectional queue for (i = 1; i < n+k; i++) {while (! Q.empty () && sum[i-1] < Sum[q.back ())//Keep the queue monotonic (incremented) q.pop_back (); Q.empty () && i-k > Q.front ())//exceeds the length of k to eliminate the element preceding the queue Q.pop_front (); Q.push_back (i-1); if (ans < sum[i]-sum[q.front ()])//Assume that the current value is greater than ans to update the value of ANS (//record, sum[n]-sum[m] The result is n-1 to m+1 and ans = sum[i]-sum[q.front ()];head = Q.front () +1;end = i;}} if (end > N)//Mark point is greater than n then loop end%=n;if (Head > N)//Mark point is greater than n then loop head%=n;printF ("%d%d%d\n", ans,head,end);} return 0;}



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

HDU 3415 Max Sum of max-k-sub-sequence (monotone queue)

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.