#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 — ...
随机推荐
- 【神经网络与深度学习】caffe+VS2013+Windows无GPU快速配置教程
首先来一波地址: happynear大神的第三方caffe:http://blog.csdn.net/happynear/article/details/45372231 Neil Z大神的第三方ca ...
- [转帖]预警 | Linux 爆“SACK Panic”远程DoS漏洞,大量主机受影响
预警 | Linux 爆“SACK Panic”远程DoS漏洞,大量主机受影响 https://cloud.tencent.com/developer/article/1447879 所有的 版本 ...
- tp5框架用foreach循环时候报Indirect modification of overloaded element of think\paginator\driver\Bootst错误
thinkphp5使用paginator分页查询数据后,需要foreach便利处理某一字段的数据,会出现类似题目的错误.主要是因为tp5使用分页类读取的数据不是纯数组的格式!所以在循环的时候需要用数据 ...
- 初探CSS - 5 创建
CSS 创建 当读到一个样式表时,浏览器会根据它来格式化 HTML 文档. 如何插入样式表 插入样式表的方法有三种: 外部样式表(External style sheet) 内部样式表(Interna ...
- Redis5以上版本伪集群搭建(高可用集群模式)
redis集群需要至少要三个master节点,我们这里搭建三个master节点,并且给每个master再搭建一个slave节点,总共6个redis节点,这里用一台机器(可以多台机器部署,修改一下ip地 ...
- centos7搭建docker+k8s集成
1. 关闭防火墙 # systemctl stop firewalld # systemctl disable firewalld # setenforce 2. 使用yum安装etcd和kubern ...
- Django新建第一个投票应用
1:在Pycharm的命令端口,确保执行文件路径是与mysite同级 $ python3 manage.py startapp polls 系统会自动生成polls应用的目录,其结构如下 polls/ ...
- docker常用技巧
1:运行中容器如何保存为一个镜像? docker commit 容器名字 镜像名字 2:怎么给容器增加名字 docker rename 容器id(或名字)name(新名字) 3:docker中的Doc ...
- Uber回馈开源的一些软件
AresDB AresDB 是 Uber 开源的一个基于 GPU 运算的实时分析存储引擎和查询引擎.具备低查询延迟.高数据刷新率和高效内存和磁盘存储管理.AresDB 要求 CUDA Toolkit ...
- composer 被墙后镜像设置
这一步主要更改镜像,不从外网直接取,现在改成了中国的一家镜像站.就是下面这个地址. https://packagist.phpcomposer.com#阿里云的composer镜像源composer ...