[CEOI2018]Global warming
[CEOI2018]Global warming
题目大意:
给定\(n(n\le2\times10^5)\),你可以将任意\(a_{l\sim r}(1\le l\le r\le n)\)每一个元素加上一个\(d(|d|\le x)\),求\(a\)的LIS长度。
思路:
不难发现区间加一个数是为了使得LIS中间一段比左边一段大,而右边一段本来就比中间的大,那么我们给右边一段同时加上也不会影响答案。因此我们可以给一个后缀加上\(x\)求LIS即可。
用两个树状数组分别维护,当前是否是已被增加过的后缀,前缀LIS的最大值。
时间复杂度\(\mathcal O(n\log n)\)。
源代码:
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=2e5+1;
int a[N],tmp[N*2];
class SegmentTree {
private:
int val[N*2];
int lowbit(const int &x) const {
return x&-x;
}
public:
void modify(int p,const int &x) {
for(;p<=tmp[0];p+=lowbit(p)) {
val[p]=std::max(val[p],x);
}
}
int query(int p) const {
int ret=0;
for(;p;p-=lowbit(p)) {
ret=std::max(ret,val[p]);
}
return ret;
}
};
SegmentTree t[2];
int main() {
const int n=getint(),x=getint();
for(register int i=1;i<=n;i++) {
tmp[i]=a[i]=getint();
tmp[n+i]=a[i]+x;
}
std::sort(&tmp[1],&tmp[n*2]+1);
tmp[0]=std::unique(&tmp[1],&tmp[n*2]+1)-&tmp[1];
for(register int i=1;i<=n;i++) {
const int pos1=std::lower_bound(&tmp[1],&tmp[tmp[0]]+1,a[i]+x)-tmp;
t[1].modify(pos1,t[0].query(pos1-1)+1);
t[1].modify(pos1,t[1].query(pos1-1)+1);
const int pos0=std::lower_bound(&tmp[1],&tmp[tmp[0]]+1,a[i])-tmp;
t[0].modify(pos0,t[0].query(pos0-1)+1);
}
printf("%d\n",t[1].query(tmp[0]));
return 0;
}
[CEOI2018]Global warming的更多相关文章
- BZOJ5442: [Ceoi2018]Global warming
BZOJ5442: [Ceoi2018]Global warming https://lydsy.com/JudgeOnline/problem.php?id=5442 分析: 等价于后缀加(前缀减也 ...
- BZOJ 5442: [Ceoi2018]Global warming
[l,r]+x不如[l,n]+x [l,r]-x不如(r,n)+x 所以等价于只有[l,n]+x 枚举断点树状数组合并 难度在于想到这个贪心 #include<cstdio> #inclu ...
- 10324 Global Warming dfs + 二分
时间限制:1000MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: G++;GCC Description Global warming is a big prob ...
- 【BZOJ4149】[AMPPZ2014]Global Warming 单调栈+RMQ+二分
[BZOJ4149][AMPPZ2014]Global Warming Description 给定一个序列a[1],a[2],...,a[n].请从中选出一段连续子序列,使得该区间最小值唯一.最大值 ...
- bzoj4149: [AMPPZ2014]Global Warming
头都烂了怎么头疼啊 考虑先做出对于一个位置以它作为唯一最小值的最远区间,这个可以单调栈上二分搞出来 那么对于一个位置这个区间而言,一定是选择这个区间的最大数是作为最终的唯一最大数最优的 为什么呢?我们 ...
- Emergency(山东省第一届ACM省赛)
Emergency Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Kudo’s real name is not Kudo. H ...
- Moscow Pre-Finals Workshop 2016. National Taiwan U Selection
A. As Easy As Possible 每个点往右贪心找最近的点,可以得到一棵树,然后倍增查询即可. 时间复杂度$O((n+m)\log n)$. #include <bits/stdc+ ...
- sentence patterns
第四部分 推理题 1.世界上每个角落的每个人都有立场,都有背景,都有推理性,能推理出一个人语言的真意,才成就了真正的推理能力: 2.换言之,如果你能通过一个人的说话推理出其身份职业,你的推理能 ...
- AMPPZ2014
[AMPPZ2014]The Lawyer 记录每天结束的最早的会议以及开始的最晚的会议即可. #include<cstdio> #define N 500010 int n,m,i,d, ...
随机推荐
- spring boot 配置数据源
以postgreSQL为例,方便下次直接使用. 其中pom.xml引入如下依赖. <?xml version="1.0" encoding="UTF-8" ...
- 在线Python学习网站
目前我们使用的Python集成环境是Anaconda3,然后使用Jupyter Notebook和Spyder两个开发环境 Goole推出了在线的开发环境,在线网站: https://colab.re ...
- 一些常见修改命令(针对ubuntu 14.04 持续更新中...)
1.PS1 在哪: echo $PS1 vi /etc/bash.bashrc /W输出最后一个目录 /w输出完整目录 2.设置静态IP地址:vim /etc/network/i ...
- SCI写作经典替换词
- CUDA性能优化----warp深度解析
本文转自:http://blog.163.com/wujiaxing009@126/blog/static/71988399201701224540201/ 1.引言 CUDA性能优化----sp, ...
- JAVA中Collection接口和Map接口的主要实现类
Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同的元素 ...
- C# 压缩文件 的创建
using System;using System.IO.Compression; using System.Collections.Generic;using System.Linq;using S ...
- 一个无锁消息队列引发的血案(三)——地:q3.h 与 RingBuffer
目录 (一)起因 (二)混合自旋锁 (三)q3.h 与 RingBuffer (四)RingQueue(上) 自旋锁 (五)RingQueue(中) 休眠的艺术 (六)RingQueue(中) 休眠的 ...
- PHP+mysql系统报错:PHP message: PHP Warning: Unknown: Failed to write session data (files)
PHP+mysql系统报错:PHP message: PHP Warning: Unknown: Failed to write session data (files) 故障现象,后台页面点击没有 ...
- 读写分离MYSQL类
2014年4月27日 12:34:08 概述: 1. 根据sql语句判断是连接读库还是写库 2. 链式调用$this->where()->get() 3. 不同的主机对应不同的实例, 不再 ...