[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的更多相关文章

  1. cf1132G. Greedy Subsequences(线段树)

    题意 题目链接 Sol 昨天没想到真是有点可惜了. 我们考虑每个点作为最大值的贡献,首先预处理出每个位置\(i\)左边第一个比他大的数\(l\),显然\([l + 1, i]\)内的数的后继要么是\( ...

  2. 【CF1132G】Greedy Subsequences(线段树)

    [CF1132G]Greedy Subsequences(线段树) 题面 CF 题解 首先发现选完一个数之后选择下一个数一定是确定的. 对于每个数预处理出左侧第一个比他大的数\(L\),那么这个数加入 ...

  3. [Codeforces1132G]Greedy Subsequences——线段树+单调栈

    题目链接: Codeforces1132G 题目大意:给定一个序列$a$,定义它的最长贪心严格上升子序列为$b$满足若$a_{i}$在$b$中则$a_{i}$之后第一个比它大的也在$b$中.给出一个数 ...

  4. Codeforces 1132G Greedy Subsequences [线段树]

    洛谷 Codeforces 看到题解那么少就来发一篇吧-- 思路 看完题目一脸懵逼,感觉无从下手. 莫名其妙地想到笛卡尔树,但笛卡尔树好像并没有太大作用. 考虑把笛卡尔树改一下:每个点的父亲设为它的右 ...

  5. Greedy Subsequences CodeForces - 1132G

    我们从右往左滑动区间, 假设dp[i]表示i为左端点时的最大长度, 通过观察可以发现, 每添加一个点, 该点$dp$值=它右侧第一个比它大位置处$dp$值+1, 但是每删除一个点会将所有以它为根的$d ...

  6. Solution -「CF 1132G」Greedy Subsequences

    \(\mathcal{Description}\)   Link.   定义 \(\{a\}\) 最长贪心严格上升子序列(LGIS) \(\{b\}\) 为满足以下两点的最长序列: \(\{b\}\) ...

  7. 【LeetCode】贪心 greedy(共38题)

    [44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...

  8. USACO . Greedy Gift Givers

    Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...

  9. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. [译]Ocelot - Service Discovery

    原文 你可以指定一个service discovery provider,ocelot将使用它来找下游的host和port. Consul 下面的配置要放在GlobalConfiguration中.如 ...

  2. zipline框架--简介

    Zipline is a Pythonic algorithmic trading library. It is an event-driven system for backtesting. Zip ...

  3. Emit 自动生成IL代码,注入代码

    Spring 框架中的注入代码,以及自动生成对接口的实现,则根据il代码注入 Emit学习(1)-Emit概览 一.Emit概述 Emit,可以称为发出或者产生.在Framework中,与Emit相关 ...

  4. ABP代码生成器与升级到VS2017VSIX

    首先,我不是要分享一个代码生成器,而是怎么升级到VS2017,简单介绍下 如何将2015的VSIX项目升级到2017 阳光铭睿 写了一篇<分享一个与ABP配套使用的代码生成器源码>,并在群 ...

  5. 二十三种设计模式之原型模式的C#实现

    原型模式就是通过拷贝快速创建一个新的对象 本例UML如图 ColorBase [Serializable] public abstract class ColorBase { public int R ...

  6. python学习第34天

    # 互斥锁# 进程之间的数据共享 # 关于数据安全的问题# 进程池(自己了解,后面线程部分还会讲) # from multiprocessing import Pool# 线程的概念 (面试的重点)# ...

  7. 基于Spring Security OAuth2搭建的Spring Cloud 认证中心

    Github传送门:https://github.com/13babybear/bounter-springcloud 实现功能有: 整合JWT 刷新Token 自定义客户端储存 自定义用户储存 资源 ...

  8. 初学python之路-day12

    本篇补上字符串的比较:按照从左往右比较每一个字符,通过字符对应的ascii进行比较 一.函数默认值的细节 # 如果函数的默认参数的默认值为变量,在所属函数定义阶段一执行就被确定为当时变量存放的值 a ...

  9. Docker----与Asp.net core 的完美结合,在docker容器中创建Asp.Net Core 项目

    在腾讯云上买了一个小容量的服务器,搭建一个docker环境后,可以尝试做一些单系统做起来很麻烦的东西.譬如说,你在windows OS或UbuntuOS中,突然想玩CentOS了,你可以选择将电脑再装 ...

  10. webpack 学习小结

    webpack 是一个模块打包工具(前提要安装 node使用npm来安装webpack) 1.安装webpack,webpack-cli , webpack-dev-server //全局安装 npm ...