【CF1132G】Greedy Subsequences(线段树)

题面

CF

题解

首先发现选完一个数之后选择下一个数一定是确定的。

对于每个数预处理出左侧第一个比他大的数\(L\),那么这个数加入进来之后\([L+1,i]\)的答案都会增加一,拿线段树维护一下就行了。

#include<iostream>
#include<cstdio>
using namespace std;
#define MAX 1000100
inline int read()
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
int a[MAX],n,K,St[MAX],top,L[MAX];
#define lson (now<<1)
#define rson (now<<1|1)
int t[MAX<<2],tag[MAX<<2];
void puttag(int now,int w){t[now]+=w;tag[now]+=w;}
void pushdown(int now)
{
if(!tag[now])return;
puttag(lson,tag[now]);puttag(rson,tag[now]);
tag[now]=0;
}
void Modify(int now,int l,int r,int L,int R,int w)
{
if(L<=l&&r<=R){puttag(now,w);return;}
int mid=(l+r)>>1;pushdown(now);
if(L<=mid)Modify(lson,l,mid,L,R,w);
if(R>mid)Modify(rson,mid+1,r,L,R,w);
t[now]=max(t[lson],t[rson]);
}
int Query(int now,int l,int r,int L,int R)
{
if(L<=l&&r<=R)return t[now];
int mid=(l+r)>>1,ret=0;pushdown(now);
if(L<=mid)ret=max(ret,Query(lson,l,mid,L,R));
if(R>mid)ret=max(ret,Query(rson,mid+1,r,L,R));
return ret;
}
int main()
{
n=read();K=read();
for(int i=1;i<=n;++i)a[i]=read();
for(int i=1;i<=n;++i)
{
while(top&&a[St[top]]<a[i])--top;
L[i]=St[top];St[++top]=i;
}
for(int i=1;i<=n;++i)
{
Modify(1,1,n,L[i]+1,i,1);
if(i>=K)printf("%d ",Query(1,1,n,i-K+1,i));
}
return 0;
}

【CF1132G】Greedy Subsequences(线段树)的更多相关文章

  1. cf1132G. Greedy Subsequences(线段树)

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

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

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

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

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

  4. [CF1132G]Greedy Subsequences

    [CF1132G]Greedy Subsequences 题目大意: 定义一个序列的最长贪心严格上升子序列为:任意选择第一个元素后,每次选择右侧第一个大于它的元素,直到不能选为止. 给定一个长度为\( ...

  5. HDU 2227 Find the nondecreasing subsequences (线段树)

    Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/3 ...

  6. ACM学习历程——HDU2227 Find the nondecreasing subsequences(线段树 && dp)

    Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., ...

  7. cf1132G 线段树解分区间LIS(一种全新的线段树解LIS思路)+单调栈

    /* 给定n个数的数列,要求枚举长为k的区间,求出每个区间的最长上升子序列长度 首先考虑给定n个数的数列的LIS求法:从左往右枚举第i点作为最大点的贡献, 那么往左找到第一个比a[i]大的数,设这个数 ...

  8. [BZOJ5139][Usaco2017 Dec]Greedy Gift Takers 权值线段树

    Description Farmer John's nemesis, Farmer Nhoj, has NN cows (1≤N≤10^5), conveniently numbered 1…N. T ...

  9. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

随机推荐

  1. mininet安装过程记录

    参考文档: http://www.brianlinkletter.com/set-up-mininet/ https://github.com/mininet/mininet/wiki/FAQ#x11 ...

  2. android java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout

    造成这个问题的原因是在xml文件中出现了重复依赖,何为重复依赖,如下:   以上便叫重复依赖 转载请标明出处:http://www.cnblogs.com/tangZH/p/8386978.html

  3. spring学习总结——装配Bean学习四(导入和混合配置)

    情景:在典型的Spring应用中,我们可能会同时使用自动化和显式配置(JavaConfig)或者XML配置,幸好在Spring中,这些配置方案都不是互斥的.你尽可以将JavaConfig的组件扫描和自 ...

  4. MySQL5.5.51启用网络远程连接

    在其它电脑主机上访问时提示host ip is not allowed to connect to this mysql 下面代码为解决该问题的方法: :\Program Files\mysql-\b ...

  5. Cs231n-assignment 2作业笔记

    assignment 2 assignment2讲解参见: https://blog.csdn.net/BigDataDigest/article/details/79286510 http://ww ...

  6. 公共的JS组件-告别CURD

    urls.py urlpatterns = [ url('^asset.html$', views.AssetView.as_view()), url('^asset-json.html$', vie ...

  7. Nginx 配置 https

    从云服务提供商处申请证书 申请 https 证书教程-百度经验 申请下来的证书目录结构 . ├── Apache │   ├── 1_root_bundle.crt │   ├── 2_website ...

  8. java格式化

    http://tool.oschina.net/apidocs/apidoc?api=jdk-zh https://www.jianshu.com/p/c8f16cab35e1# 参考官方的 api说 ...

  9. django 视图模式

    一 视图 FBV --- function based view(基于函数视图) CBV --- class based view(基于类的视图函数) 二 请求方式 get post put/patc ...

  10. Linux下配置nfs并远程挂载实战探讨

    简单介绍: nfs是网络文件系统,允许一个节点通过网络访问远程计算机的文件系统,远程文件系统可以被直接挂载到本地,文件操作和本地没有区别,如果是局域网的nfs那么io的性能也可以保证 nfs是Netw ...