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): 5690 Accepted Submission(s): 2059
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
求長度不超過k的最大連續子序列。
維護字首和,把字首和加入單調佇列,對於每一個下標,查詢單調佇列裡的最小值,然後做差就可以得到以這個下標結尾的最優解。
程式碼:
/* ***********************************************
Author :_rabbit
Created Time :2014/5/13 2:06:57
File Name :C.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 1LL<<60
#define eps 1e-8
#define pi acos(-1.0)
typedef __int64 ll;
ll a[201000],sum[200100],que[200100];
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
ll m,n,T;
cin>>T;
while(T--){//維護字首和。
scanf("%I64d%I64d",&n,&m);
for(ll i=1;i<=n;i++)scanf("%I64d",&a[i]),a[i+n]=a[i];
sum[0]=0;for(ll i=1;i<=2*n;i++)sum[i]=sum[i-1]+a[i];
ll ans=-INF,start,end;
ll head=0,tail=0,p;que[tail++]=0;
for(ll i=1;i<=2*n;i++){
p=max(0LL,i-m);
while(que[head]<p&&head<tail)head++;//彈出距離i大於m的點。
if(sum[i]-sum[que[head]]>ans){//對於以i結尾的所有序列中,找單調佇列中最小的一個元素做差,這樣就可以得到以這個元素為結尾的最大和。
ans=sum[i]-sum[que[head]];
start=que[head]+1;end=i;
}
while(head<tail&&sum[que[tail-1]]>sum[i])tail--;//維護一個遞增的單調佇列。
que[tail++]=i;
}
if(start>n)start-=n;
if(end>n)end-=n;
printf("%I64d %I64d %I64d\n",ans,start,end);
}
return 0;
}
相關文章
- hdu3415 單調佇列求區間最大和佇列
- HDU3415 Max Sum of Max-K-sub-sequence (DP+單調佇列)佇列
- HDU 3530 單調佇列佇列
- hdu 3401 單調佇列+DP佇列
- hdu 4122 單調佇列佇列
- hdu4374單調佇列+dp佇列
- HDU 3530 Subsequence (dp+單調佇列)佇列
- HDU3530 單調佇列的應用佇列
- 單調佇列佇列
- 單調棧/單調佇列佇列
- 單調佇列雙端佇列佇列
- hdu 4122Alice's mooncake shop(單調佇列)佇列
- 單調棧 和 單調佇列佇列
- 單調棧和單調佇列佇列
- POJ 2823 單調佇列佇列
- HDU 4123 Bob's Race:樹的直徑 + 單調佇列 + st表佇列
- 單調佇列最佳化 DP佇列
- [POJ 3415] Common Substrings (字尾陣列+單調棧優化)陣列優化
- POJ 3017 單調佇列dp佇列
- 佇列-單端佇列佇列
- hdu5040 優先佇列+bfs佇列
- noi.ac #289. 電梯(單調佇列)佇列
- poj3017 dp+單調佇列佇列
- HDU 5884-Sort(佇列+二分)佇列
- hdu5380 貪心+雙端佇列佇列
- 多重揹包問題的單調佇列優化佇列優化
- POJ 3415-Common Substrings(字尾陣列+單調棧-公共子串的長度)陣列
- C語言 簡單的佇列(陣列佇列)C語言佇列陣列
- [學習筆記] 單調佇列最佳化DP - DP筆記佇列
- 【BFS+優先佇列】HDU 3442 Three Kingdoms佇列
- HDU4546 比賽難度 (優先佇列)佇列
- hdu 4546 優先佇列 數列組合和第m小佇列
- POJ 2823Sliding Window(單調佇列水題)佇列
- HDU5630 Hiking(貪心+優先佇列)佇列
- RabbitMQ-簡單佇列MQ佇列
- 單向鏈式佇列佇列
- 棧,佇列,優先順序佇列簡單介面使用佇列
- POJ2823 Sliding Window (單調佇列的基本應用)佇列