HDU-3415-Max Sum of Max-K-sub-sequence(单调队列,带限制的最大子段和)
2024-10-07 06:44:29
链接:
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;
}
最新文章
- 解决Cannot delete or update a parent row: a foreign key constraint fails (`current_source_products`.`product_storage`, CONSTRAINT `product_storage_ibfk_3` FOREIGN KEY (`InOperatorID`)
- ooize简介
- JQ获取当前是第几个元素,以及直接选取第几个元素的方法
- maven仓库介绍
- 2013年7月28日web前端学习笔记-------head相关标签应用
- Unity3d 如何找到游戏对象并改变其颜色
- mount的艺术
- zoj-3782-Ternary Calculation
- VB中的GDI编程-2 画笔
- java使用线程请求訪问每次间隔10分钟连续5次,之后停止请求
- K-means聚类 的 Python 实现
- WeakHashMap
- 【CF1009F】Dominant Indices(长链剖分)
- 解决QtCreator中文乱码
- 1.7Oob成员变量和局部变量疑难区分
- Windows Server 2008 R2之六活动目录域服务的卸载
- saltstack安装配置及常用命令
- JS 实现关闭浏览器
- web前端 html/css总结点
- HDU3001 Travelling
热门文章
- Kubernetes 中文文档
- 网易云课堂_C++程序设计入门(下)_第10单元:月映千江未减明 – 模板_第10单元 - 单元作业:OJ编程 - 创建数组类模板
- Oracle常用操作表空间sql脚本
- C#学习笔记一(概念,对象与类型,继承)
- Angular5 父组件获取子组件实例( ViewChildren、ViewChild用法)
- C# StreamReader与StreamWriter
- java 集合 队列(Queue)
- 开启linux服务器防火墙
- java实现顺序队列
- python基础之迭代器生成装饰器