HDU 3415
Max Sum of Max-K-sub-sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5616 Accepted Submission(s): 2024
Problem Description
Given a circle sequence A[1],A[2],A[3]......A[n]. Circle sequence means the left neighbour of A[1] is A[n] , and the right neighbour 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.
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.
Input
The 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).
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).
Output
For each test case, you should 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
4
6 3
6 -1 2 -6 5 -5
6 4
6 -1 2 -6 5 -5
6 3
-1 2 -6 5 -5 6
6 6
-1 -1 -1 -1 -1 -1
Sample Output
7 1 3
7 1 3
7 6 2
-1 1 1
Author
shǎ崽@HDU
Source
Recommend
lcy
RMQ 尝试失败。。。
单调队列,sum[i] 代表 1 ~ i 的累加值
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 6 using namespace std; 7 8 const int MAX_N = 100005; 9 const int INF = 1e9; 10 int a[2 * MAX_N],sum[2 * MAX_N]; 11 int N,K; 12 int deque[2 * MAX_N]; 13 14 int cal(int x) { 15 return x > N ? x - N : x; 16 } 17 18 void solve() { 19 int anssum = -INF,anss,anse; 20 int s = 0,e = 0; 21 for(int i = 0; i <= 2 * N; ++i) { 22 if(s != e) { 23 int t = sum[i] - sum[ deque[s]]; 24 if(anssum < t || anssum == t && cal(deque[s] + 1) < anss || 25 anssum == t && cal(deque[s] + 1) == anss && (i - deque[s]) < (anse - anss + 1)) { 26 anssum = t; 27 anss = cal(deque[s] + 1); 28 anse = cal(i); 29 } 30 } 31 32 while(s < e && sum[i] < sum[ deque[e - 1] ]) --e; 33 deque[e++] = i; 34 35 if(deque[s] == i - K) ++s; 36 } 37 38 printf("%d %d %d\n",anssum,anss,anse); 39 } 40 41 int main() 42 { 43 //freopen("sw.in","r",stdin); 44 int t; 45 scanf("%d",&t); 46 47 while(t--) { 48 scanf("%d%d",&N,&K); 49 memset(sum,0,sizeof(sum)); 50 for(int i = 1; i <= N; ++i) { 51 scanf("%d",&a[i]); 52 } 53 for(int i = N + 1; i <= 2 * N; ++i) { 54 a[i] = a[i - N]; 55 } 56 57 for(int i = 1; i <= 2 * N; ++i) { 58 sum[i] += sum[i - 1] + a[i]; 59 } 60 solve(); 61 62 } 63 //cout << "Hello world!" << endl; 64 return 0; 65 }