[CF1132G]Greedy Subsequences
[CF1132G]Greedy Subsequences
题目大意:
定义一个序列的最长贪心严格上升子序列为:任意选择第一个元素后,每次选择右侧第一个大于它的元素,直到不能选为止。
给定一个长度为\(n(n\le10^6)\)的序列\(A\),同时给定一个常数\(k\),求该序列的所有长度为\(k\)的子区间的最长贪心严格上升子序列的长度。
思路:
不难发现,若将每个点和右侧第一个大于它的元素相连,可以得到一个森林,其中每个结点对应的父结点为序列中右侧第一个大于它的元素。若没有区间长度为\(k\)的限制,答案就是深度最大结点的深度。
有\(k\)的限制时,可以将DFS序弄出来,用线段树维护。每次加入右端点时,将子树所有结点对应权值\(+1\)。此时权值代表的就是其在森林中的深度。
而删除左结点时,不妨将对应子树的权值重置为\(0\),而就算它后面又被\(+1\),也不会超过目前的根结点的权值,因此不会对结果产生影响。
时间复杂度\(\mathcal O(n\log n)\)。
源代码:
#include<cstdio>
#include<cctype>
#include<vector>
#include<functional>
#include<ext/pb_ds/priority_queue.hpp>
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=1e6+1;
std::vector<int> e[N];
inline void add_edge(const int &x,const int &y) {
e[x].push_back(y);
}
int a[N],in[N],out[N],tot;
__gnu_pbds::priority_queue<std::pair<int,int>,std::greater<std::pair<int,int>>> q;
void dfs(const int &x) {
in[x]=++tot;
for(int y:e[x]) {
dfs(y);
}
out[x]=tot;
}
class SegmentTree {
#define _left <<1
#define _right <<1|1
#define mid ((b+e)>>1)
private:
int max[N<<2],tag[N<<2];
void push_down(const int &p) {
if(!tag[p]) return;
if(tag[p _left]!=-1) {
max[p _left]+=tag[p];
tag[p _left]+=tag[p];
}
if(tag[p _right]!=-1) {
max[p _right]+=tag[p];
tag[p _right]+=tag[p];
}
tag[p]=0;
}
void push_up(const int &p) {
max[p]=std::max(max[p _left],max[p _right]);
}
public:
void add(const int &p,const int &b,const int &e,const int &l,const int &r) {
if(tag[p]==-1) return;
if(b==l&&e==r) {
max[p]++;
tag[p]++;
return;
}
push_down(p);
if(l<=mid) add(p _left,b,mid,l,std::min(mid,r));
if(r>mid) add(p _right,mid+1,e,std::max(mid+1,l),r);
push_up(p);
}
void clear(const int &p,const int &b,const int &e,const int &l,const int &r) {
if(tag[p]==-1) return;
if(b==l&&e==r) {
max[p]=0;
tag[p]=-1;
return;
}
push_down(p);
if(l<=mid) clear(p _left,b,mid,l,std::min(mid,r));
if(r>mid) clear(p _right,mid+1,e,std::max(mid+1,l),r);
push_up(p);
}
int query() const {
return max[1];
}
#undef _left
#undef _right
#undef mid
};
SegmentTree t;
int main() {
const int n=getint(),k=getint();
for(register int i=1;i<=n;i++) {
a[i]=getint();
while(!q.empty()&&q.top().first<a[i]) {
add_edge(i,q.top().second);
q.pop();
}
q.push({a[i],i});
}
while(!q.empty()) {
dfs(q.top().second);
q.pop();
}
for(register int i=1;i<k;i++) {
t.add(1,1,n,in[i],out[i]);
}
for(register int i=k;i<=n;i++) {
t.add(1,1,n,in[i],out[i]);
if(i>k) t.clear(1,1,n,in[i-k],out[i-k]);
printf("%d%c",t.query()," \n"[i==n]);
}
return 0;
}
[CF1132G]Greedy Subsequences的更多相关文章
- cf1132G. Greedy Subsequences(线段树)
题意 题目链接 Sol 昨天没想到真是有点可惜了. 我们考虑每个点作为最大值的贡献,首先预处理出每个位置\(i\)左边第一个比他大的数\(l\),显然\([l + 1, i]\)内的数的后继要么是\( ...
- 【CF1132G】Greedy Subsequences(线段树)
[CF1132G]Greedy Subsequences(线段树) 题面 CF 题解 首先发现选完一个数之后选择下一个数一定是确定的. 对于每个数预处理出左侧第一个比他大的数\(L\),那么这个数加入 ...
- [Codeforces1132G]Greedy Subsequences——线段树+单调栈
题目链接: Codeforces1132G 题目大意:给定一个序列$a$,定义它的最长贪心严格上升子序列为$b$满足若$a_{i}$在$b$中则$a_{i}$之后第一个比它大的也在$b$中.给出一个数 ...
- Codeforces 1132G Greedy Subsequences [线段树]
洛谷 Codeforces 看到题解那么少就来发一篇吧-- 思路 看完题目一脸懵逼,感觉无从下手. 莫名其妙地想到笛卡尔树,但笛卡尔树好像并没有太大作用. 考虑把笛卡尔树改一下:每个点的父亲设为它的右 ...
- Greedy Subsequences CodeForces - 1132G
我们从右往左滑动区间, 假设dp[i]表示i为左端点时的最大长度, 通过观察可以发现, 每添加一个点, 该点$dp$值=它右侧第一个比它大位置处$dp$值+1, 但是每删除一个点会将所有以它为根的$d ...
- Solution -「CF 1132G」Greedy Subsequences
\(\mathcal{Description}\) Link. 定义 \(\{a\}\) 最长贪心严格上升子序列(LGIS) \(\{b\}\) 为满足以下两点的最长序列: \(\{b\}\) ...
- 【LeetCode】贪心 greedy(共38题)
[44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...
- USACO . Greedy Gift Givers
Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
随机推荐
- Python——将高德坐标(GCJ02)转换为GPS(WGS84)坐标
# 官方API: http://lbs.amap.com/api/webservice/guide/api/convert # 坐标体系说明:http://lbs.amap.com/faq/top/c ...
- python epoll方式tcp连接回发消息
# -*- coding:utf-8 -*- import socket import select class testserver(): def __init__(self): self.serv ...
- Eclipse 开发设置编码格式--4个修改地方完美
背景:本人用这么久,因为大部分都是设定为UTF-8 就可以了,但是一些老项目居然是GBK格式,所以 工作空间.通常文件类型的编码都是UTF-8. 针对特殊项目设定特定格式,实际中本人对整个项目设定并不 ...
- 搜索表字段包含某字符串的SQL和监控Oracle数据库的SQL。
1.第一个SQL 背景:需要找到SQL Server数据库中,包含某个字符串的表,输出表和包含该字符串的列. )='=' --这里填要搜索的字符串 DECLARE @sql NVARCHAR(MAX) ...
- 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能
题目:编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能 要求:MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.bin 用来把文本文件(内容为十 ...
- java.lang.IllegalArgumentException: Invalid source 'classpath:spring-mvc.xml'
今天在跑项目时遇到java.lang.IllegalArgumentException: Invalid source 'classpath:spring-mvc.xml'报错,自己也是摸索了很久,一 ...
- 「JOISC 2018 Day 3」比太郎的聚会
题解: 很套路的题目 我们按照询问中的不算的个数是否大于$block$分类 如果大于,就$O(n)dp$一下 如果小于,就预处理出到每个点前$block$小的点 $block取\sqrt{n}$的话复 ...
- 今日头条Marketing API小工具(.Net Core版本)
前言 由于工作原因,需要用到今日头条的Marketing API做一些广告投放的定制化开发.然后看现在网上也没多少关于头条Marketing API的文章,于是便就有了该篇文章. 头条Marketin ...
- maven发布到tomcat报错: Publishing failed Could not publish to the server. java.lang.IndexOutOfBoundsException
eclipse中将maven项目发布到tomcat报错时: Publishing failed Could not publish to the server. java.lang.IndexOutO ...
- python re库的正则表达式学习笔记
1. 安装 默认已经安装好了python环境了 re库是python3的核心库,不需要pip install,直接import就行 2. 最简单的模式 字符本身就是最简单的模式 比如:'A', 'I ...