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): 7034 Accepted Submission(s): 2589
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.
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).
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
7 1 3
7 6 2
-1 1 1
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = ;
int num[MAXN << ];
int Q[MAXN << ];
int sum[MAXN << ];
int main(){
int T, n, k;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &k);
int head = , tail = -;
sum[] = ;
for(int i = ; i <= n; i++){
scanf("%d", num + i);
sum[i] = sum[i - ] + num[i];
}
for(int i = n + ; i <= n + k; i++){
num[i] = num[i - n];
sum[i] = sum [i - ] + num[i];
}
int ans = -0x3f3f3f3f, L = , R = ;
for(int i = ; i < n + k; i++){
while(head <= tail && sum[i - ] < sum[Q[tail]])
tail--;
while(head <= tail && i - Q[head]> k)head++;
Q[++tail] = i - ;
if(sum[i] - sum[Q[head]] > ans){
ans = sum[i] - sum[Q[head]];
L = Q[head] + ;
R = i;
}
}
printf("%d %d %d\n", ans, L>n?L-n:L, R>n?R-n:R);
}
return ;
}
贪心wa;由于规定了最大长度,这样贪心就不行了;
wa代码 :
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = ;
int num[MAXN];
int main(){
int T;
int n, k;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &k);
for(int i = ; i < n; i++){
scanf("%d", num + i);
}
for(int i = n; i < * n; i++)
num[i] = num[i - n];
int l = , r = , ans = -0x3f3f3f3f, cur = , L = , R = , cnt = ;
for(int i = ; i < * n; i++){
cur += num[i];
cnt++;
if(cur <= num[i] || cnt > k){
l = i;
cur = num[i];
cnt = ;
} r = i;
if(cur > ans){
L = l;
R = r;
ans = cur;
}
}
printf("%d %d %d\n", ans, L + > n ? L + - n: L + , R + > n ? R + - n: R + );
}
return ;
}
Max Sum of Max-K-sub-sequence(单调队列)的更多相关文章
- poj3017 Cut the Sequence 单调队列 + 堆 dp
描述 把一个正数列 $A$分成若干段, 每段之和 不超过 $M$, 并且使得每段数列的最大值的和最小, 求出这个最小值. 题目链接 题解 首先我们可以列出一个$O(n^2)$ 的转移方程 : $F_i ...
- $Poj3017\ Cut\ The\ Sequence$ 单调队列优化$DP$
Poj AcWing Description 给定一个长度为N的序列 A,要求把该序列分成若干段,在满足“每段中所有数的和”不超过M的前提下,让“每段中所有数的最大值”之和最小. N<=10 ...
- POJ 3709 K-Anonymous Sequence (单调队列优化)
题意:给定一个不下降数列,一个K,将数列分成若干段,每段的数字个数不小于K,每段的代价是这段内每个数字减去这段中最小数字之和.求一种分法使得总代价最小? 思路:F[i]表示到i的最小代价.f[i]=m ...
- hdu 1003 Max Sum (DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 3415 单调队列
Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- Subsequence(两个单调队列)
Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- Parade(单调队列优化dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 3530 单调队列
题目大意:给你n个数, 让你问你最长的满足要求的区间有多长,区间要求:MAX - MIN >= m && MAX - MIN <= k 思路:单调队列维护递增和递减,在加入 ...
- POJ - 1821 单调队列优化DP + 部分笔记
题意:n个墙壁m个粉刷匠,每个墙壁至多能被刷一次,每个粉刷匠要么不刷,要么就粉刷包含第Si块的长度不超过Li的连续墙壁(中间可不刷),每一块被刷的墙壁都可获得Pi的利润,求最大利润 避免重复粉刷: 首 ...
- [luoguP2569] [SCOI2010]股票交易(DP + 单调队列)
传送门 $f[i][j]$ 表示第i天,手中股票数为j的最优解 初始化 $f[i][0]=0$ $0<=i<=n$ 4种方式转移 以前没买过,第i天凭空买 $f[i][j]=-j*ap$ ...
随机推荐
- Android: 在WebView中获取网页源码
1. 使能javascript: ? 1 webView.getSettings().setJavaScriptEnabled(true); 2. 编写本地接口 ? 1 2 3 4 5 final c ...
- android避免service被杀
1.在service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建@Override public int onStartComma ...
- OC特有语法-分类(category)
本文转载Keefo. Objective-C的Object-oriented programming特性提供subclass和category这2个比较非常重要的部分.subclass应该反复被各种编 ...
- eclipse项目转android studio详解
第一步:项目导入 向AS中导入项目的方法有两种(其实是一种). 方法一:是在eclipse中先导出为gradle(如图1),然后打开AS,找到项目中的gradle文件,直接导入. 方法二:直接在AS中 ...
- compass模块----Utilities----Sprites精灵图合图
css雪碧图又叫css精灵或css sprite,是一种背景图片的拼合技术.使用css雪碧图,能够减少页面的请求数.降低图片占用的字节,以此来达到提升页面访问速度的目的.但是它也有令人诟病的地方,就是 ...
- 2.IKAnalyzer 中文分词器配置和使用
一.配置 IKAnalyzer 中文分词器配置,简单,超简单. IKAnalyzer 中文分词器下载,注意版本问题,貌似出现向下不兼容的问题,solr的客户端界面Logging会提示错误. 给出我配置 ...
- (转)添加服务引用和添加Web引用对比
在WindowsForm程序中添加服务引用和Web引用对比 为了验证书上有关Visual Studio 2010添加服务引用和Web引用的区别,进行实验. 一.建立一个Web服务程序项目新建项目,选择 ...
- linux syslog详解
linux syslog详解 分三部分 一.syslog协议介绍 二.syslog函数 三.linux syslog配置 一.syslog协议介绍 1.介绍 在Unix类操作系统上,syslog广 ...
- cocos2dx 音效 粒子 数据存储
1.音效 引入头文件 #include "simpleAudioEngine.h" 命名空间 using namespace CocosDenshion; //创建一个背景 Siz ...
- CUICatalog: Invalid asset name supplied:
[UIImage imageNamed:name];但是这个name却是空的,所以就报了这个错了. 解决方法,在项目中搜索UIImage imageNamed:,然后打印看看所谓的name是否为空.找 ...