Max Sum of Max-k-sub-sequence
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 6844 Accepted Submission (s): 2518
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 in Put 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). outputfor Each test case, you should output a line contains three int Egers, the Max Sum in the sequence, the start position 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 Input4 6 3 6-1 2-6 5-5 6 4 6-1 2-6 5-5 6 3-1 2-6 5-56 6 6-1 -1-1 -1-1 -1 sample Output7 1 3 7 1 3 7 6 2-1 1 1 key: Lets find the longest string length less than or equal to K; violence timeout; Timeout code:
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>using namespacestd;Const intinf=0x3f3f3f3f;#defineMem (x, y) memset (x,y,sizeof (x))#defineSI (x) scanf ("%d", &x)#definePI (x) printf ("%d", X)#defineSD (x) scanf ("%lf", &x)#defineP_ printf ("")typedefLong LongLL;Const intmaxn=100010;intM[MAXN];intMain () {intt,n,k; SI (T); while(t--) {si (N); Si (K); for(intI=1; i<=n;i++) SI (M[i]); intCur=0, ans=-Inf,l,r; for(intI=1; i+k-1<=n;i++) {cur=0; for(intj=0; j<k;j++) {cur+=m[i+J]; if(cur>ans) {ans=cur; L=i;r=i+J; }}} printf ("%d%d%d\n", Ans,l,r); } return 0;}
Max Sum of Max-k-sub-sequence