CSU 1553 Good subsequence(RMQ问题 + 二分)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1553
Description
Give you a sequence of n numbers, and a number k you should find the max length of Good subsequence. Good subsequence is a continuous subsequence of the given sequence and its maximum value - minimum value<=k. For example n=5, k=2, the sequence ={5, 4, 2, 3, 1}. The answer is 3, the good subsequence are {4, 2, 3} or {2, 3, 1}.
Input
There are several test cases.
Each test case contains two line. the first line are two numbers indicates n and k (1<=n<=10,000, 1<=k<=1,000,000,000). The second line give the sequence of n numbers a[i] (1<=i<=n, 1<=a[i]<=1,000,000,000).
The input will finish with the end of file.
Output
For each the case, output one integer indicates the answer.
Sample Input
5 2
5 4 2 3 1
1 1
1
Sample Output
3
1
Hint
Source
题意:
给你一个n个数,在这些数里面找一段连续区间,这个区间需要满足这个区间里的最大值-最小值 <= k。找出最大的区间长度。
题解:
拿到题的时候,就想到RMQ来找区间最值,二分长度找最大值。
然后队伍分工合作,一个人写二分,我写RMQ。一不小心写错了一个点wa了一次,QAQ。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
#define eps 0.0000001
#define LNF (1<<60)
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = 1e9+; LL st_max[][maxn], st_min[][maxn];
LL A[maxn];
void RMQ_init(int n){
for(int i=;i<n;i++) st_max[][i]=st_min[][i]= A[i];
for(int j = ;(<<j) <= n;j++){
int k = <<(j-);
for(int i=;i+k<n;i++){
st_max[j][i] = max(st_max[j-][i], st_max[j-][i+k]);
st_min[j][i] = min(st_min[j-][i], st_min[j-][i+k]);
}
}
}
LL RMQ(int a, int b){
int dis = abs(b-a) + ;
int k;
for(k=;(<<k)<=dis;k++);
k--;
// printf("%lld %lld\n", max(st_max[k][a], st_max[k][b-(1<<k)+1]), min(st_min[k][a], st_min[k][b-(1<<k)+1]));
return max(st_max[k][a], st_max[k][b-(<<k)+])-min(st_min[k][a], st_min[k][b-(<<k)+]);
}
int test(int len, int n, int k)
{
for(int i = len-; i<n; i++){
if(RMQ(i-len+, i)<=1LL*k)
return ;
}
return ;
}
int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif // LOCAL int n, k;
while(~scanf("%d%d", &n, &k)){
ms(st_max, );
ms(st_min, );
ms(A, );
for(int i=;i<n;i++) scanf("%lld", &A[i]);
RMQ_init(n);
// printf("%lld\n", RMQ(0, n-1));
int l = , r = n+;
int mid, ans;
while(l<r){
mid = (l+r)/;
if(test(mid, n, k)){
ans = mid;
l = mid+;
}
else
r = mid;
}
printf("%d\n", ans);
}
return ;
}
模板题。XD
CSU 1553 Good subsequence(RMQ问题 + 二分)的更多相关文章
- STL or Force --- CSU 1553: Good subsequence
Good subsequence Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1553 Mean: 给你一个长 ...
- csu 1553(RMQ+尺取法)
1553: Good subsequence Time Limit: 2 Sec Memory Limit: 256 MBSubmit: 794 Solved: 287[Submit][Statu ...
- 1553: Good subsequence (很奇妙的set模拟题,也可以直接暴力)
1553: Good subsequence Submit Page Summary Time Limit: 2 Sec Memory Limit: 256 Mb Subm ...
- Good subsequence( RMQ+二分)
Description Give you a sequence of n numbers, and a number k you should find the max length of Good ...
- [HDOJ5726]GCD(RMQ,二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 题意:给定数列,求区间[L,R]的GCD的值,并求出有多少个子区间满足和[L,R]的GCD相等. ...
- Subsequence poj 3061 二分(nlog n)或尺取法(n)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9236 Accepted: 3701 Descr ...
- Subsequence(暴力+二分)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10875 Accepted: 4493 Desc ...
- HDU - 6406 Taotao Picks Apples (RMQ+dp+二分)
题意:N个高度为hi的果子,摘果子的个数是从位置1开始从左到右的严格递增子序列的个数.有M次操作,每次操作对初始序列修改位置p的果子高度为q.每次操作后输出修改后能摘到得数目. 分析:将序列分为左.右 ...
- 题解报告:poj 3061 Subsequence(前缀+二分or尺取法)
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
随机推荐
- Java JDK安装教程以及JDK多版本间快速切换配置
原本想自己写一篇,结果在网上发现一篇写的特别好的博文,大家可以去原网址围观浏览加点赞, 只是搬运工+迷弟. 原文地址:https://blog.csdn.net/qq_38916130/article ...
- TensorFlow学习笔记6-数值计算基础
TensorFlow学习笔记6-数值计算 本笔记内容为"数值计算的基础知识".内容主要参考<Deep Learning>中文版. \(X\)表示训练集的矩阵,其大小为m ...
- Model Inversion Attack Paper Indexpage
Paper [1]: White-box neural network attack, adversaries have full access to the model. Using Gradien ...
- 安全运维 - Linux系统攻击应急响应
Linux 应急相应 - 总纲 应急准备: 制定应急策略 组建应急团队 其他应急资源 安全事件处理: 痕迹数据获取 分析.锁定攻击源删除可疑账号关闭异常进程.端口禁用相应异常开机启动项删除异常定时任务 ...
- git --> 工作使用流程
[git]------git开发过程中的使用流程------[WangQi] 001.创建仓库 002.新建项目 003.初始化仓库 这一步不需要做 git init : 文件夹中会多出一个隐藏 ...
- CEPH安装(CentOS 7)
以包含四个节点的集群为例,其中包括一个 ceph-deploy 管理节点和一个三节点的Ceph存储集群. 下图中每个节点代表一台机器. 安装 CEPH 部署工具 执行如下命令: sudo yum in ...
- [Python3 填坑] 018 组装类的几个例子
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 MetaClass 举例 2.2 type 举例 2.3 MetaClass 举例 1. print( 坑的信息 ) 挖坑时间:2019 ...
- Python学习-第一天-函数和模块的使用
目录 Python学习-第一天总结 print输出的一种简单格式 函数参数之可变参数 模块管理函数 if else语句的单行实现(简洁) 变量作用域 函数书写格式 Python学习-第一天总结 pri ...
- 最长回文子序列/最长回文子串(DP,马拉车)
字符子串和字符子序列的区别 字符字串指的是字符串中连续的n个字符:如palindrome中,pa,alind,drome等都属于它的字串 而字符子序列指的是字符串中不一定连续但先后顺序一致的n个字符: ...
- vue数据响应式的一些注意点
有关对象属性值不触发视图更新的情况: Vue 不能检测到对象属性的添加或删除,由于 Vue 会在初始化实例时对属性执行 getter/setter 转化过程,所以属性必须在 data 对象上存在才能让 ...