Max sum of max-K-Sub-Sequence
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 4983 accepted submission (s): 1820 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
Authorsh @ HDU
Sourcehdoj monthly contest-2010.06.05
A ring sequence with N numbers (-1000 .. 1000, n <= 10 ^ 5) is given, so that you can find one and the largest continuous subsequence. The length of this continuous subsequence is less than or equal to K.
Idea: the problem is converted to max (sum [I-j] (I-j <= K). This is a monotonous queue problem. Enumeration I: maintain an incremental sequence with a monotonous queue. It is enough to retrieve the first line of each J.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # define maxn 200005 using namespace STD; int n, m, K, ANS, S, E; int A [maxn], sum [maxn]; int sp, EP; int C [maxn], Id [maxn]; void push (int kk) // The monotonic queue maintains an incremental sequence {int I, j, T; t = sum [Kk]; I = EP; while (I> = sp & T <= C [I]) I --; // note that I> = Sp begins to WA Ep = I + 1; c [EP] = T; Id [EP] = KK; while (KK-ID [Sp]> = k) SP ++;} void solve () {int I, j; SP = Ep = 0; C [0] = 0; Id [0] = 0; ans =-1000000000; for (I = 1; I <= m; I ++) {If (ANS <sum [I]-C [Sp]) {ans = sum [I]-C [Sp]; S = ID [Sp] + 1; E = I; If (S> N) S-= N; If (E> N) E-= N ;} push (I) ;}} int main () {int I, j, T; scanf ("% d", & T); While (t --) {scanf ("% d", & N, & K); sum [0] = 0; for (I = 1; I <= N; I ++) {scanf ("% d", & A [I]); sum [I] = sum [I-1] + A [I];} M = N + K; for (I = n + 1; I <= m; I ++) {sum [I] = sum [I-1] + A [I-n];} solve (); printf ("% d \ n", ANS, S, e) ;}return 0 ;}