hdu 3415 Max Sum of Max-K-sub-sequence 单调队列。
2024-08-25 17:08:50
Max Sum of Max-K-sub-sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5335 Accepted Submission(s): 1939
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
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
7 1 3
7 6 2
-1 1 1
Author
shǎ崽@HDU
Source
Recommend
#include<iostream>
#include<stdio.h>
#include<cstdlib>
#include<cstring>
#include<cstdlib>
using namespace std; int a[],s[];
int head,tail,len,n,k;
typedef struct
{
int sum;
int s,e;
}Queue;
Queue q[],tom,tmp; void Init()
{
int i;
for(i=;i<=n;i++)
scanf("%d",&a[i]);
len=n+k;
for(i=n+;i<=len;i++)
a[i]=a[i-n];
for(s[]=,i=;i<=len;i++)
s[i]=a[i]+s[i-];
n=n+k;
len=len-k;
}
int main()
{
int T,i;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
Init();
head=;tail=;
tom.sum=s[];tom.s=;tom.e=;
q[]=tom;
for(i=;i<=n;i++)
{
tmp.sum=s[i];
tmp.s=;
tmp.e=i;
while( head<=tail && q[tail].sum>tmp.sum ) tail--;
q[++tail]=tmp;
while( head<=tail && q[head].e+k<tmp.e ) head++; if(tmp.sum-q[head].sum>tom.sum && tmp.e!=q[head].e)
{
tom.sum=tmp.sum-q[head].sum;
tom.s=q[head].e+;
tom.e=tmp.e;
}
else if( i<=k && tmp.sum>tom.sum)
{
tom=tmp;
}
}
printf("%d",tom.sum);
if( tom.s>len ) tom.s-=len;
if( tom.e>len ) tom.e-=len;
printf(" %d %d\n",tom.s,tom.e);
}
return ;
}
最新文章
- 用sass写栅格系统
- HTTP/1.1 中 If-Modified-Since 和 If-Unmodified-Since 区别简记
- ie8中支持 password 的 placeholder
- JVM 关闭前执行命令的钩子
- CAF(C++ actor framework)(序列化之复杂类,分析 还有自己不懂的细思恐极函数实现)(三)
- Android常用错误解决汇总
- 关于 Swift
- JavaScript检测提交表单text合法
- 第九周博客作业<;西北师范大学|李晓婷>;
- 动态更新Icon
- poj 2240 Arbitrage(最短路问题)
- Linux下使用NTFS格式移动硬盘
- 一道非常易错的js面试题
- OrmLite动态创建表,一个实体类创建多张表的的偏招
- [z] How can we render CSS3 in a WebBrowser Control ?
- NDK编程jni学习入门,声明native方法,使其作为java与c的交互接口
- Metro应用Json数据处理
- (转)用python实现抓取网页、模拟登陆
- 深度增强学习--Deep Q Network
- PAT 1072. 开学寄语(20) JAVA