#333 Div2 Problem B Approximating a Constant Range (尺取 && RMQ || 尺取 && multiset)
题目链接:http://codeforces.com/contest/602/problem/B
题意 :给出一个含有 n 个数的区间,要求找出一个最大的连续子区间使得这个子区间的最大值和最小值的差值不超过 1 ,最后输出这个子区间的长度。
分析 :我们可以根据区间的最值之差利用尺取的方法来找出答案=> if(差值>1) Left++; else Right++; 然后每一次右端点+1就更新一次答案直到右端点到达 n 。在这里我们需要找出区间最值,也就是经典的RMQ问题,可以利用ST算法模板进行O(nlogn)的预处理接下来进行尺取便不会超时。上述的过程完全可以使用multiset来替代,我们可以每一次把(Left, Right)区间内的数放到multiset里面,根据set容器的特性,我们能很方便得到(Left, Right)这个区间的最值,也能使用erase()在区间不满足题意进行Left++操作时将原来Left的元素在集合内剔除,也能用size()方便的得到区间的长度,但是两者都用到了尺取的思想。
瞎想 :当需要维护区间的时候需要获取最值或者获取区间长度的时候,不妨考虑能否借助set容器来实现。
Two pointers && RMQ :
#include<bits/stdc++.h>
#define LL long long
using namespace std;
;
], Min[maxn][];
inline void initialize(int & n)
{
; i<=n; i++) Max[i][] = arr[i], Min[i][] = arr[i];
; j<; j++){
; i<=n; i++){
<<j) - <= n){
Min[i][j] = min(Min[i][j-], Min[i+(<<(j-))][j-]);
Max[i][j] = max(Max[i][j-], Max[i+(<<(j-))][j-]);
}
}
}
}
int RMQ(int L, int R)
{
;
//while((1<<(k+1)) <= R-L+1) k++;
k = log2(R-L+);
<<k)+][k]);
<<k)+][k]);
return MAX - MIN;
}
int main(void)
{
int n;
scanf("%d", &n);
; i<=n; i++) scanf("%d", &arr[i]);
initialize(n);
, R = ;
;
while(R <= n){
int temp = RMQ(L, R);
){
ans = max(R-L+, ans);
R++;
}
else{
L++;
}
}
printf("%d\n", ans);
;
}
Two pointers && multiset :
#include<bits/stdc++.h>
using namespace std;
;
int arr[maxn];
int main(void)
{
int n;
scanf("%d", &n);
; i<=n; i++) scanf("%d", &arr[i]);
multiset<int> s;
s.insert(arr[]);
];
];
, ed = ;//双指针记录Left 和 Right
;
; i<=n; i++){
Max = max(Max, arr[i]);
Min = min(Min, arr[i]);
s.insert(arr[i]);
){
ans = max(ans, (int)s.size());
}else{
&& Max - Min > ){//相当于Left++操作
set<int>::iterator it = s.find(arr[st]);
s.erase(it);
st++;
set<int>::iterator it_Max = --s.end();
set<int>::iterator it_Min = s.begin();
Max = *it_Max;
Min = *it_Min;
}
ans = max(ans, (int)s.size());
}
}
printf("%d\n", ans);
;
}
#333 Div2 Problem B Approximating a Constant Range (尺取 && RMQ || 尺取 && multiset)的更多相关文章
- #333 Div2 Problem B Approximating a Constant Range(尺取法)
题目:http://codeforces.com/contest/602/problem/B 题意 :给出一个含有 n 个数的区间,要求找出一个最大的连续子区间使得这个子区间的最大值和最小值的差值不超 ...
- Codeforces Round #333 (Div. 2) B. Approximating a Constant Range st 二分
B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...
- Codeforces Round #333 (Div. 2) B. Approximating a Constant Range
B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...
- Codeforces 602B Approximating a Constant Range(想法题)
B. Approximating a Constant Range When Xellos was doing a practice course in university, he once had ...
- FZU 2016 summer train I. Approximating a Constant Range 单调队列
题目链接: 题目 I. Approximating a Constant Range time limit per test:2 seconds memory limit per test:256 m ...
- cf602B Approximating a Constant Range
B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...
- codeforce -602B Approximating a Constant Range(暴力)
B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...
- 【32.22%】【codeforces 602B】Approximating a Constant Range
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【CodeForces 602C】H - Approximating a Constant Range(dijk)
Description through n) and m bidirectional railways. There is also an absurdly simple road network — ...
随机推荐
- react 中 EventEmitter 事件总线机制
此机制可用于 react 中兄弟组件中的通信 npm install events -S 事件总线: // eventBus.js import {EventEmitter} from 'events ...
- abp.event.on与abp.event.off使用
apb的全局事件 var eventName = "app.createOrEditFieldModalSaved"; var reloadPage = function () { ...
- java实现稀疏数组压缩
package sparseArray; public class SparseArray { public static void main(String[] args) { // TODO Aut ...
- centos7 源码编译安装 nginx
安装步骤 下载 nginx 源码包 官网 $ wget http://nginx.org/download/nginx-1.16.0.tar.gz 解压 nginx 压缩包 $ tar -zxvf n ...
- Spring jar包详解(转)
spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到spring-moc ...
- DAX/PowerBI系列 - 累计总计(Cumulative Total, Running Total)
DAX/PowerBI系列 - 累计总计(Cumulative Total) 2017/07/23 更新:B列公式(见最后) 2019/08/08 更新:在可视化数据的时候,一定要选择日期维度的日期列 ...
- 定义一个Book类,有书名,价格,作者等信息。定义相应的方法来改变这些属性的值。定义一个方法来显示Book的所有信息。
package com.fs.test; public class Test { public static void main(String[] args) { // 先声明后赋值 book b;/ ...
- linux 配置环境变量三种方式
一:用于当前终端: export PATH=$PATH:<你的要加入的路径> 此方式仅用于当前终端,一旦该终端关闭,则配置失效 二:用于当前用户: vi ~/.bashrc 然后加入:ex ...
- vue.js(4)--字符串跑马灯
制作一个字符串的跑马灯效果 (1)实例代码 <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- Ajax轮询请求
Ajax轮询请求 什么是轮询? 轮询(polling):客户端按规定时间定时向服务端发送ajax请求,服务器接到请求后马上返回响应信息并关闭连接. Ajax轮询需要服务器有很快的处理速度与快速响应. ...