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.
 
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).
 
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;
}

最新文章

  1. Mifare系列5-存储结构(转)
  2. Spring的Lifecycle
  3. Objective-C学习笔记-第一天(2)
  4. Unity3D之Mecanim动画系统学习笔记(四):Animation State
  5. Dan计划:重新定义人生的10000个小时 - 阮一峰的网络日志
  6. 单例模式 - 程序实现(Java)
  7. 手把手教你使用spring cloud+dotnet core搭建微服务架构:服务治理(-)
  8. bzoj2118(加法原理)(墨墨的等式)
  9. android开发之this.finish()的使用
  10. (NO.00001)iOS游戏SpeedBoy Lite成形记(十三)
  11. 浅析 .Net Core中Json配置的自动更新
  12. Oracle Database 10g安装
  13. node中间层转发请求
  14. 梳理vue双向绑定的实现原理
  15. comparable和comparator
  16. [人工智能] 安装python jupyter
  17. 性能测试&#160;Apache参数配置与性能调优
  18. git push 失败
  19. CButtonST|CUniButton等按钮类的使用
  20. JSP--TOMCAT-MYSQL web页面添加

热门文章

  1. 快充 IC BQ25896 如何判斷 手機插著 adapter 充電器時,adapter Iout 大於限制,adapter Vout 小於 限制,導致 battery 不但沒充電且還需放電。
  2. 使用android ndk编译boost动态库
  3. zoj 2760(网络流+floyed)
  4. LeetCode OJ--ZigZag Conversion
  5. python安装numpy和matplotlib
  6. Ural 1774 Barber of the Army of Mages 最大流
  7. 将压缩包文件(rar/zip)伪装成图片(jpg/gif/png/ico)
  8. awk如何区分shell脚本传进来的参数和自身的参数?awk如何获取shell脚本传进来的参数;awk中如何执行shell命令
  9. myBatis-plus异常提示For input string: &quot;{0=null}&quot;
  10. Android二维码工具zxing使用