链接:

https://vjudge.net/problem/HDU-3415

题意:

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.

思路:

单调队列,维护一个上升的前缀和,最大值就是Sum[i]-Sum[j],而取不到位置从前面pop,比当前位置大的值从后面pop.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL; const int MAXN = 1e5+10;
const int INF = 1e9; int a[MAXN*2];
int Sum[MAXN*2];
int n, k; int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
memset(Sum, 0, sizeof(Sum));
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i], a[i + n] = a[i];
for (int i = 1; i <= n*2; i++)
Sum[i] = Sum[i - 1] + a[i];
deque<pair<int, int> > que;
int res = Sum[1], l=0, r=1;
for (int i = 0; i <= 2 * n; i++)
{
while (!que.empty() && i-que.front().first > k)
que.pop_front();
while (!que.empty() && Sum[i] < que.back().second)
que.pop_back();
if (!que.empty() && Sum[i]-que.front().second > res)
{
res = Sum[i]-que.front().second;
l = que.front().first;
r = i;
// cout << l << ' ' << r << endl;
}
que.push_back(make_pair(i, Sum[i]));
}
cout << res << ' ' << ((l+1) > n ? l+1-n:l+1) << ' ' << (r > n?r-n:r) << endl;
} return 0;
}

HDU-3415-Max Sum of Max-K-sub-sequence(单调队列,带限制的最大子段和)的更多相关文章

  1. HDU 4123 Bob's Race:树的直径 + 单调队列 + st表

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4123 题意: 给你一棵树,n个节点,每条边有长度. 然后有m个询问,每个询问给定一个q值. 设dis[ ...

  2. HDU 4193 Non-negative Partial Sums(想法题,单调队列)

    HDU 4193 题意:给n个数字组成的序列(n <= 10^6).求该序列的循环同构序列中,有多少个序列的随意前i项和均大于或等于0. 思路: 这题看到数据规模认为仅仅能用最多O(nlogn) ...

  3. HDU 5945 / BestCoder Round #89 1002 Fxx and game 单调队列优化DP

    Fxx and game 问题描述   青年理论计算机科学家Fxx给的学生设计了一款数字游戏. 一开始你将会得到一个数\:XX,每次游戏将给定两个参数\:k,tk,t, 任意时刻你可以对你的数执行下面 ...

  4. HDU 3410 &amp;&amp; POJ 3776 Passing the Message 单调队列

    题意: 给定n长的数组(下标从1-n)(n个人的身高,身高各不同样 问:对于第i个人,他能看到的左边最矮的人下标.(假设这个最矮的人被挡住了,则这个值为0) 还有右边最高的人下标,同理若被挡住了则这个 ...

  5. hdu 1003 Max Sum (DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)   ...

  6. HDU 1081:To The Max

    To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  7. hdu 3415 单调队列

    Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. HDU 3530 单调队列

    题目大意:给你n个数, 让你问你最长的满足要求的区间有多长,区间要求:MAX - MIN >= m && MAX - MIN <= k 思路:单调队列维护递增和递减,在加入 ...

  9. HDU 3415 Max Sum of Max-K-sub-sequence 最长K子段和

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=3415 意甲冠军:环.要找出当中9长度小于等于K的和最大的子段. 思路:不能採用最暴力的枚举.题目的数据量是 ...

随机推荐

  1. jmeter响应数据Unicode编码转换为汉字

    2018-07-09     10:24:34 每次用jmeter做接口测试时,响应信息中文总是显示Unicode编码格式,每次都要在网上寻找这一段转换的代码,但是我发现在网上找这段代码有点麻烦,像我 ...

  2. python3.5以后venv创建/激活/退出虚拟环境

    1.创建虚拟环境 $ python3 -m venv <环境名称> 2.激活虚拟环境 $ source <环境名称>/bin/activate 3.关闭虚拟环境 $ deact ...

  3. 用DotNetDetour HOOK .net类库

    https://github.com/bigbaldy1128/DotNetDetour ------------------------------------------------------- ...

  4. jenkins 配置 gitlab webhook 实现自动发布

    测试环境需要git提交代码后,Jenkins自动部署,需要gitlab配置project webhook. 1,Jenkins版本2.89  gitlab 8.11 2,Jenkins需要安装插件:G ...

  5. Nginx的用途

    Nginx应该是现在最火的web和反向代理服务器,没有之一.她是一款诞生于俄罗斯的高性能web服务器,尤其在高并发情况下,相较Apache,有优异的表现. 那除了负载均衡,她还有什么其他的用途呢,下面 ...

  6. 【VS开发】使用WinPcap编程(3)——设置过滤器

    设置过滤器要用到两个函数,一个是pcap_compile(),另一个是pcao_setfilter().他们的函数原型如下所示: int pcap_compile (pcap_t *p, struct ...

  7. 关于Docx动态控制word模板文件的数据

    博客:https://www.cnblogs.com/24klr/ github: https://github.com/luoruiemail/Dynamic_Word_Web 参考资料:https ...

  8. 解决SQLPLUS ??? 显示的临时办法

    错误现象为: 解决命令 export NLS_LANG=american_america.zhs16gbk

  9. [转帖].NET Core单文件发布静态编译AOT CoreRT

    .NET Core单文件发布静态编译AOT CoreRT https://www.cnblogs.com/linezero/p/CoreRT.htm .NET Core单文件发布静态编译AOT Cor ...

  10. jira:恢复数据:AO_187CCC_SIDEBAR_LINK

    JIRA 恢复数据时报错 ,关键词是找不到 AO_187CCC_SIDEBAR_LINK. 经网上查为 mysql connect jar 包 的版本过高所致. 降低版本后,成功导入数据.