[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 ...
随机推荐
- Keras的一些功能函数
摘自: https://www.cnblogs.com/Anita9002/p/8136357.html 1.模型的信息提取 # 节点信息提取 config = model.get_config() ...
- react中根据后台值动态配置
业务中我们要实现对应的数据是1是男,2是女,这就要根据键来进行动态匹配,通常后台来给你一个1或者2,你来进行匹配,这样的数据一般在表格中比较常见. <Card title="Mock- ...
- Linux二进制安装apache2.4.25
Linux二进制安装apache2.4.25 安装环境:CentOS 6.2 先检查是否安装了Apache 如通是通过rpm包安装的话直接用下面的命令:rpm -q httpd 也可以使用如下两种方法 ...
- CentOS7离线安装mysql5.7
下载mysql5.7,系统选择redhat,版本选择RHEL7,下载RPM Bundle后得到一个tar文件.这里得到文件mysql-5.7.25-1.el7.x86_64.rpm-bundle.ta ...
- 转 原生js canvas实现苹果电脑mac OS窗口最小化效果
http://www.17sucai.com/pins/demo-show?id=2459 http://www.17sucai.com/pins/demo-show?id=2458 很多资料 ,前 ...
- java操作mongodb & springboot整合mongodb
简单的研究原生API操作MongoDB以及封装的工具类操作,最后也会研究整合spring之后作为dao层的完整的操作. 1.原生的API操作 pom.xml <!-- https://mvnre ...
- pwnable.tw silver_bullet
产生漏洞的原因 int __cdecl power_up(char *dest) { char s; // [esp+0h] [ebp-34h] size_t new_len; // [esp+30h ...
- 解决关于win10下eclipse代码格式化不生效问题
今日,在写代码的时候遇到在eclipse中ctrl+shift+f格式化代码不生效的问题,原本以为是和热键冲突,所以关闭了搜狗输入法的简体和繁体的切换方式,但是发现,还是没有生效,所以,想到修改ecl ...
- python正则表达式--findall、finditer方法
findall方法 相比其他方法,findall方法有些特殊.它的作用是查找字符串中所有能匹配的字符串,并以结果存于列表中,然后返回该列表 注意: match 和 search 是匹配一次 finda ...
- Error occurred during initialization of VM Incompatible initial and maximum heap sizes specified
双击Tomcat server在eclipse弹出的Tomcat配置项里面选择open launch configuration 选择arguments在 vm arguments 里面添加 -Xms ...