HDOJ 3415 Max Sum of Max-K-sub-sequence



单调队列优化。。。。

Max Sum of Max-K-sub-sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4759    Accepted Submission(s): 1734


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

Source
HDOJ Monthly Contest – 2010.06.05

Recommend
lcy


#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int a[411111];
int f[411111];
int que[1111111];
int pt[1111111];
int n,k;
int T;
int head,tail;
int sum[411111];
int max_sum,start,end;


int main()
{
scanf("%d",&T);
while (T--)
{
memset(f,0,sizeof(f));
memset(que,0,sizeof(que));
memset(pt,0,sizeof(pt));
memset(sum,0,sizeof(sum));
scanf("%d%d",&n,&k);
for (int i=1;i<=n;i++)
{
scanf("%d",&a);
a[i+n]=a;
}
for (int i=1;i<=n+k;i++)
{
sum+=sum[i-1]+a;
}

head=tail=0;
max_sum=start=end=-1e9;
for(int i=1;i<=n+k;i++)
        {
            while(head<tail&&i-pt[head]>k)  head++;
            while(head<tail&&sum[i-1]<que[tail-1]) tail--;

            que[tail]=sum[i-1]; pt[tail++]=i-1;

            f=sum-que[head];

            if(max_sum<f)
            {
                max_sum=f;
                start=pt[head]+1;
                end=i;
            }
        }

if(start>n) start-=n;
if(end>n) end-=n;

printf("%d %d %d\n",max_sum,start,end);

}
return 0;
}


转载于:https://www.cnblogs.com/CKboss/p/3350984.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/474221.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

DurOS语音开发

DuerOS语音开发项目&#xff0c;该项目通过搭载DuerOS开发者平台&#xff0c;利用平台开发的一组API规范&#xff0c;实现实时的语音播报功能。 整体流程示意图&#xff1a; 所用设备&#xff1a;树莓派微机系统、arduino与74HC595单片机、地图彩灯控制沙盘、小度语音。 数据流…

Sharepoint学习笔记—习题系列--70-573习题解析 -(Q40-Q44)

Question 40You need to send a single value from a consumer Web Part to a provider Web Part.Which interface should you use?A. IAlertNotifyHandlerB. IWebPartFieldC. IWebPartParametersD. IWebPartRow 解析&#xff1a;本题的目的是在Webpart之间传递“单值”(singl…

LeetCode 1563. 石子游戏 V(DP)

文章目录1. 题目2. 解题2.1 区间DP1. 题目 几块石子 排成一行 &#xff0c;每块石子都有一个关联值&#xff0c;关联值为整数&#xff0c;由数组 stoneValue 给出。 游戏中的每一轮&#xff1a; Alice 会将这行石子分成两个 非空行&#xff08;即&#xff0c;左侧行和右侧行…

python中进程创建—fork()

导入模块&#xff1a;import os 1.程序执行到os.fork()时&#xff0c;操作系统会创建一个新的进程&#xff08;子进程&#xff09;&#xff0c;复制父进程信息 2.子进程从fork&#xff08;&#xff09;函数中得到返回值零 3.父进程从fork&#xff08;&#xff09;函数中得到返…

个人简历小程序

为了熟练掌握微信小程序开发的一些基本技巧&#xff0c;熟悉微信小程序开发流程&#xff0c;特此&#xff0c;运用所学知识&#xff0c;做了一个个人简历小程序。 效果图如下&#xff1a; 代码如下&#xff1a; ① index.jsconst app getApp() Page({ data: { userInfo: {}…

Android的Fragment介绍

前言 fragment是从android3.0开始提出来的&#xff0c;用来支持大屏幕设备的ui设计。通过将activity划分为多个fragment&#xff0c;不仅提高了设计的灵活性&#xff0c;而且可以在程序运行时改变它们的特征&#xff0c;比如动态的修改&#xff0c;替换已有的fragment等等。 fr…

[Kaggle] Heart Disease Prediction

文章目录1. 数据探索2. 特征处理管道3. 训练模型4. 预测kaggle项目地址1. 数据探索 import pandas as pd train pd.read_csv(./train.csv) test pd.read_csv(./test.csv)train.info() test.info() abs(train.corr()[target]).sort_values(ascendingFalse)<class pandas.c…

python进程的回收—wait

1.os.wait()回收资源 os.wait()方法用来回收子进程占用的资源&#xff1a; import os import time ret os.fork() # 创建新的进程 一次调用&#xff0c;两次返回 if ret 0: # 子进程执行 # 子进程拿到的返回值是0 print("子进程&#xff1a;pid%d, ppid%d" % (…

Oracle数据库逻辑存储结构管理相关问题与解决

我们在Mysql数据库中&#xff0c;一般是登入进去一个数据库&#xff0c;紧接着就创建数据库实例&#xff1a;create databse XXX;但是在Oracle数据库就不行。在数据库连接过程中老是报监听失败的错误。在备份表空间的时候&#xff0c;设置表空间为备份模式&#xff0c;它提示我…

Python调用C的方法

参考&#xff1a;http://www.cnblogs.com/fxjwind/archive/2011/07/05/2098636.html http://amazingjxq.com/2012/01/09/python%E8%B0%83%E7%94%A8c%E5%87%BD%E6%95%B0/>>>cd /home/jxq/code/gcc -fPIC -shared bob_hash.c -o bob_hash.so然后在python里面用ctypes加载…

01.神经网络和深度学习 W2.神经网络基础

文章目录1. 二分类2. 逻辑回归3. 逻辑回归损失函数4. 梯度下降5. 导数6. 计算图导数计算7. 逻辑回归中的梯度下降8. m个样本的梯度下降9. 向量化10. 向量化的更多例子11. 向量化 logistic 回归12. 向量化 logistic 回归梯度输出13. numpy 广播机制14. 关于 python / numpy 向量…

python中的孤儿进程

1.子进程未运行完父进程就结束运行退出&#xff0c;留下来的子进程就是孤儿进程 2.父进程结束退出&#xff0c;子进程会被继父收回&#xff0c;通常是int进程&#xff08;pid为1&#xff09;无危害 import os import time ret os.fork() # 创建新的进程 一次调用&#xff0…

Oracle数据库物理存储结构管理遇到的问题与解决

问题一&#xff1a;当我创建一个重做日志文件放入重做日志文件组中的时候&#xff0c;查询数据字典发现新创建的重做日志文件的状态为“不合法”。 解决方案&#xff1a; 通过查阅相关资料了解到 新建的重做日志文件组成员状态为INVALID,这是由于新建的成员文件还没有被…

实例讲解hadoop中的map/reduce查询(python语言实现)

条件&#xff0c;假设你已经装好了hadoop集群&#xff0c;配好了hdfs并可以正常运行。 $hadoop dfs -ls /data/dw/explorerFound 1 itemsdrwxrwxrwx - rsync supergroup 0 2011-11-30 01:06 /data/dw/explorer/20111129$ hadoop dfs -ls /data/dw/explo…

python中僵尸进程

⼦进程运⾏完成&#xff0c;但是⽗进程迟迟没有进⾏回收&#xff0c;此时⼦进程实际上并没有退出&#xff0c;其仍然占⽤着系统资源&#xff0c;这样的⼦进程称为僵⼫进程。 因为僵⼫进程的资源⼀直未被回收&#xff0c;造成了系统资源的浪费&#xff0c;过多的僵⼫进程将造成…

01.神经网络和深度学习 W3.浅层神经网络

文章目录1. 神经网络概览2. 神经网络的表示3. 神经网络的输出4. 多样本向量化5. 激活函数6. 为什么需要 非线性激活函数7. 激活函数的导数8. 随机初始化作业参考&#xff1a; 吴恩达视频课 深度学习笔记 1. 神经网络概览 xW[1]b[1]}⟹z[1]W[1]xb[1]⟹a[1]σ(z[1])\left.\begin…

多进程修改全局变量

多进程中&#xff0c;每个进程中所有数据&#xff08;包括全局变量&#xff09;都各有拥有⼀份&#xff0c;互不影响 (读时共享&#xff0c;写时复制) import os import time num 100 ret os.fork() # 创建新的进程 一次调用&#xff0c;两次返回 if ret 0: # 子进程…

多进程模块multiprocessing

multiprocessing模块就是跨平台版本的多进程模块&#xff0c;提供了⼀个Process类来代表一个进程对象 创建⼦进程时&#xff0c;只需要传⼊⼀个执⾏函数和函数的参数&#xff0c;创建⼀个 Process实例&#xff0c;⽤start&#xff08;&#xff09;方法启动 &#xff0c;join()…