HDU - 3415 Max Sum of Max-K-sub-sequence
来源:互联网 发布:人工智能 感知 编辑:程序博客网 时间:2024/05/17 02:14
题意:求长度不超过K的最大的连续序列的和
思路:采用单调队列,我们要求的是Max{sum[i]-sum[j]}(i-j<=k),可以这么想每次的i,我们都在可以的范围内找个一个最小的sum[j]就是可以了,最后求最大就是了,至于怎么能够快速的找个一个最小的数,我们采用单调队列
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 1000005;const int INF = 0x3f3f3f3f;int n,k;int arr[MAXN],sum[MAXN],q[MAXN];int main(){int t;scanf("%d", &t);while (t--){scanf("%d%d", &n, &k);for (int i = 1; i <= n; i++){scanf("%d", &arr[i]);arr[i+n] = arr[i];}sum[0] = 0;for (int i = 1; i <= 2*n; i++)sum[i] = sum[i-1] + arr[i];int head = 0,tail = 0;q[head] = 0;int Max = -INF,x,y;for (int i = 1; i <= 2*n; i++){while (head <= tail && i-q[head]>k)head++;int j = q[head];if (sum[i] - sum[j] > Max){Max = sum[i] - sum[j];x = j+1;y = i;}while (head <= tail && sum[q[tail]] > sum[i])tail--;q[++tail] = i;}if (x > n)x -= n;if (y > n)y -= n;printf("%d %d %d\n", Max, x, y);}return 0;}
0 0
- hdu 3415 Max Sum of Max-K-sub-sequence
- hdu 3415 Max Sum of Max-K-sub-sequence
- hdu 3415 Max Sum of Max-K-sub-sequence题解
- hdu 3415 Max Sum of Max-K-sub-sequence
- hdu 3415 Max Sum of Max-K-sub-sequence
- HDU 3415 Max Sum of Max-K-sub-sequence
- hdu 3415 Max Sum of Max-K-sub-sequence
- HDU 3415 Max Sum of Max-K-sub-sequence
- HDU 3415 Max Sum of Max-K-sub-sequence
- HDU - 3415 Max Sum of Max-K-sub-sequence
- HDU 3415 Max Sum of Max-K-sub-sequence
- HDU 3415 Max Sum of Max-K-sub-sequence
- HDU 3415 Max Sum of Max-K-sub-sequence
- HDU 3415 Max Sum of Max-K-sub-sequence
- HDU Max Sum of Max-K-sub-sequence(单调队列)
- Max Sum of Max-K-sub-sequence HDU
- Max Sum of Max-K-sub-sequence
- Max Sum of Max-K-sub-sequence
- JAVA 基础笔记|抽象类与接口的区别
- HDU 1484 Basic wall maze (dfs + 记忆化)
- 笔记本外接显示器
- listView中嵌套viewpager随着listview滚动
- linux shell awk 流程控制语句(if,for,while,do)详细介绍
- HDU - 3415 Max Sum of Max-K-sub-sequence
- redis-benchmark 测试redis性能
- wm命令使用方法及LCD显示图标大小不正常时解决办法
- 《编程之美》之摞烙饼的排序问题
- PhoneGap在Android 手机上的全屏问题
- eclipse自动补全设置
- #ifndef、(或者#ifndef)、#def、#endif等宏这几个宏是为了进行条件编译
- linux之cut用法
- android权限大全