[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. Frp基础配置模版

    Frp基础配置模版存档,供参考: 不写注释说明了,直接上模板: frps.ini [common] bind_port = 7000 privilege_token = password vhost_ ...

  2. Redis的集群搭建

    一.集群的搭建 1.准备工作 (1)安装ruby环境 redis集群管理工具redis-trib.rb依赖ruby环境,首先需要安装ruby环境: yum -y install ruby yum -y ...

  3. Ubuntu18.04应用程序安装集锦

    整理网上的资源: Python Web开发工具箱 ubuntu美化及超NB的zsh配置 api文档查询工具:zeal,dash(收费)

  4. vue 移动端项目总结(mint-ui)

    跨域解决方案 config/dev.env.js 'use strict' const merge = require('webpack-merge') const prodEnv = require ...

  5. Flsk-Bootstrap-2

    目录 Flsk-Bootstrap-2 结构 解压Bootstrap 制作基础模板 视图函数 初始文件 启动文件 浏览器 Flsk-Bootstrap-2 参考:Flask 项目中使用 bootstr ...

  6. nginx Access-Control-Allow-Origin 多域名跨域设置

    2019-1-16 12:24:15 星期三 网站的静态文件(js, css, 图片, 字体等)是在一个单独的域名下的, 为了防止非法访问, 给nginx添加了跨域的控制, 也可以在PHP代码中添加 ...

  7. 在Cyclone IVE中使用进位链的几个规则

    最近在FPGA上做ps级的Delay line,所以认真剖析了一下Cyclone IVE4的布局布线延迟.这里说明CARRY链的几个特性规则,如有错误请各位大大指出,谢谢.(另外由于匆忙没有时间验证其 ...

  8. A - Alice's Print Service ZOJ - 3726 (二分)

    Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using h ...

  9. The Apache Tomcat installation at this directory is version 8.5.40. A Tomcat 8.0 installation is expected.

    问题描述 Eclipse 配置 Apache Tomcat 8.5.40(8.0.x 以上版本),会报如下错误信息: 解决方法 1)在 Apache Tomcat 的安装目录中找到 lib 目录下的 ...

  10. python脚本--mysql数据库升级、备份

    在公司经常要做测试环境的升级.备份.维护:升级后台的应用,不可避免要进行数据库的升级与备份,花了一个上午琢磨了一个脚本分享给大家. ToB的业务,在做环境维护的时候,有初始化环境和增量升级的环境,在测 ...