Max sum of max-K-Sub-Sequence

Source: Internet
Author: User
Document directory
  • Problem description
  • Input
  • Output
  • Sample Input
  • Sample output
  • Author
  • Source
Problem descriptiongiven 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. 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 two integers n, k (1 <= n <= 100000, 1 <= k <= N ), then n integers followed (all the integers are between-1000 and 1000 ). outputfor 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
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
Authornotonlysuccess @ hdusourcehdoj monthly contest-2010.06.05 struggled with the question of the day and thought wrong at first .. I thought it was just done .. Later I figured it out that it was wrong, and then I used the monotonous queue to solve it perfectly! Ans = max (sum [I]-min (sum [I-K]) adopts monotonous queue maintenance in the interval [I-K, the sum value of I-1] is monotonous, And the monotonous queue is very powerful! # Include <iostream> # include <algorithm> using namespace STD; # define n 200020 # define INF 0 xfffffffstruct node {int index, num;}; node Dia [N]; int Q [2 * n], sum [N]; int main () {int I, j, T, Head, tail; int N, K; scanf ("% d", & T); While (t --) {int S, E, ANS =-INF; sum [0] = 0; scanf ("% d", & N, & K); for (I = 1; I <= N; I ++) {scanf ("% d ", & Dia [I]. num); If (ANS <Dia [I]. num) {ans = Dia [I]. num; S = I; E = I;} Dia [I]. index = I; sum [I] = sum [I-1] + Dia [I]. num;} for (I = 1; I <K; I ++) {Dia [I + N]. index = I + N; Dia [I + N]. num = Dia [I]. num; sum [I + N] = sum [I + n-1] + Dia [I + N]. num;} Dia [0]. index = 0; Dia [0]. num = 0; head = N; tail = N; Q [+ head] = 0; int temp; for (I = 1; I <= N + k-1; I ++) {temp = sum [I]; if (I <= K & Ans <temp) {ans = temp; S = 1; E = I ;} while (DIA [Q [head]. index <I-k) head --; temp-= sum [Q [head]; If (ANS <temp) {ans = temp; S = Q [head] + 1; E = I ;} while (tail <Head & (sum [Q [tail + 1]> sum [I] | Q [tail + 1] <I-k) tail ++; Q [tail --] = I;} cout <ans <"" <S <"<(E> n? E-N: E) <Endl;} return 0 ;}

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.