首页 技术 正文
技术 2022年11月15日
0 收藏 539 点赞 3,203 浏览 2315 个字

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 DescriptionGiven 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. 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 two integers N , K(1<=N<=100000 , 1<=K<=N), then N integers followed(all the integers are between -1000 and 1000). OutputFor 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

 Authorshǎ崽@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;
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,111
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922