[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. 《.NET手札》

    第一记 .net是一个平台,即.NET Framework平台 c#是基于.net平台的开发语言 .net的两种交互模式: B/S : 即浏览器(Browser)/服务器模式(Server)     ...

  2. luogu P5303 [GXOI/GZOI2019]逼死强迫症

    传送门 只有两行,考虑递推,设\(f_i\)为没有那两个\(1*1\)的,前\(i\)列的方案,可以发现一次可以放一个竖的或两个横的,也就是\(f_i=f_{i-1}+f_{i-2}\) 再设\(g_ ...

  3. windows查看进程信息

    wmic process where caption="java.exe" get processid,caption,commandline /value

  4. JDK8源码阅读之Collection及相关方法

    最近面试总会被问到JDK8中的一些新特性,所以闲下来抽时间看了一下8的源码,目前主要看的是数据结构部分,特此记录一下. 新增函数式接口,实现该接口的可以直接用lambda表达式. default和st ...

  5. JavaScript的数组和循环

    1.  数组: a)         声明数组:var 数组名 = new Array(数组大小); Var  emp=new Array(“AA”,“BB”,“CC”): b)         添加 ...

  6. 记事本:一些js案例以及DOM和BOM

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 2018-2019-2 20165221『网络对抗技术』Exp4:恶意代码分析

    2018-2019-2 20165221『网络对抗技术』Exp4:恶意代码分析 实验要求: 是监控你自己系统的运行状态,看有没有可疑的程序在运行. 是分析一个恶意软件,就分析Exp2或Exp3中生成后 ...

  8. Linux-ubuntu16.04安装 mysql5.7-PHP7.0+Swoole

    步骤1 – 安装MySQL 在 Ubuntu 16.04 中,默认情况下,只有最新版本的 MySQL 包含在 APT 软件包存储库中.只需更新服务器上的包索引并安装默认包 apt-get. sudo ...

  9. P5304 [GXOI/GZOI2019]旅行者

    题目地址:P5304 [GXOI/GZOI2019]旅行者 这里是官方题解 一个图 \(n\) 点 \(m\) 条边,里面有 \(k\) 个特殊点,问这 \(k\) 个点之间两两最短路的最小值是多少? ...

  10. ubuntu系统检测端口占用情况

    参考链接: https://blog.csdn.net/qwfys200/article/details/80837036 命令: $ sudo netstat -tupln