Max Sum of Max-k-sub-sequence
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 6277 Accepted Submission (s): 2289
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 Input46 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 Output7 1 37 1 37 6 2-1 1 1
Authorshǎ Cub @hdu
Sourcehdoj Monthly contest–2010.06.05
#include <iostream> #include <cstring> #include <cstdio> #include <string> #include <cmath > #include <algorithm> #include <cstdlib> #include <cmath> #include <queue> #include < Vector> #include <set>using namespace std; #define INF 100000000int Tt,n,k;int sum[200005],a[200005],hd,ed;int Main () {scanf ("%d", &TT); while (tt--) {scanf ("%d%d", &n,&k); int j=n; sum[0]=0; for (int i=1;i<=n;i++) {scanf ("%d", &a[i]); Sum[i]=sum[i-1]+a[i]; } for (int i=n+1;i<n+k;i++) {sum[i]=sum[i-1]+a[i-n]; } n=n+k-1; int ans=-inf; Deque<int> Q; Q.clear (); for (int i=1;i<=n;i++) {while (!q.empty () &&sum[i-1]<sum[q.back ()))//Maintain monotonicity Q. Pop_back (); while (!q.empty () &&q.front () <i-k) Q.pop_front (); Q.push_back (i-1); if (Sum[i]-sum[q.front ()]>ans) {Ans=sum[i]-sum[q.front ()]; Hd=q.front (+1); Ed=i; }} if (Ed>j) ed=ed%j; printf ("%d%d%d\n", ans,hd,ed); } return 0;}
Monotonic Queue Optimization DP