我们从右往左滑动区间, 假设dp[i]表示i为左端点时的最大长度, 通过观察可以发现, 每添加一个点, 该点$dp$值=它右侧第一个比它大位置处$dp$值+1, 但是每删除一个点会将所有以它为根的$dp$值全-1, 所以可以根据转移建一棵树, 需要有单点查询单点更新以及树链加, 可以用线段树维护dfs序$O(logn)$实现, 或者直接树剖$O(nlog^2n)$

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head
#ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, k;
vector<int> g[N], q;
int a[N], L[N], R[N], dp[N], fa[N], ans[N];
int val[N<<2], tag[N<<2];
int no[N]; void dfs(int x) {
L[x]=++*L,no[*L]=x;
for (int y:g[x]) dfs(y);
R[x]=*L;
}
void pd(int o) {
if (tag[o]) {
tag[lc]+=tag[o];
tag[rc]+=tag[o];
val[lc]+=tag[o];
val[rc]+=tag[o];
tag[o]=0;
}
}
void upd1(int o, int l, int r, int ql, int qr, int v) {
if (ql<=l&&r<=qr) return val[o]+=v,tag[o]+=v,void();
pd(o);
if (mid>=ql) upd1(ls,ql,qr,v);
if (mid<qr) upd1(rs,ql,qr,v);
val[o]=max(val[lc],val[rc]);
}
void upd2(int o, int l, int r, int x, int v) {
if (l==r) return val[o]=v,void();
pd(o);
if (mid>=x) upd2(ls,x,v);
else upd2(rs,x,v);
val[o]=max(val[lc],val[rc]);
}
int qry(int o, int l, int r, int x) {
if (l==r) return val[o];
pd(o);
if (mid>=x) return qry(ls,x);
return qry(rs,x);
}
void dfs(int o, int l, int r) {
if (l==r) printf("no=%d,a=%d,val=%d\n",no[l],a[no[l]],val[o]);
else pd(o),dfs(ls),dfs(rs);
} int main() {
scanf("%d%d", &n, &k);
REP(i,1,n) scanf("%d", a+i);
a[++n] = INF;
q.pb(n);
PER(i,1,n-1) {
while (a[i]>=a[q.back()]) q.pop_back();
int j = q.back();
g[j].pb(i), fa[i] = j, q.pb(i);
}
dfs(n);
PER(i,1,n-1) {
if (i+k<n) upd1(1,1,n,L[i+k],R[i+k],-1);
if (fa[i]-i>=k) dp[i] = 1;
else dp[i] = qry(1,1,n,L[fa[i]])+1;
upd2(1,1,n,L[i],dp[i]);
if (i+k<=n) ans[i]=val[1];
}
REP(i,1,n-k) printf("%d ", ans[i]);hr;
}

Greedy Subsequences CodeForces - 1132G的更多相关文章

  1. [CF1132G]Greedy Subsequences

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

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

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

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

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

  4. Solution -「CF 1132G」Greedy Subsequences

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

  5. cf1132G. Greedy Subsequences(线段树)

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

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

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

  7. Codeforces 1132G(关系转化树+dfn+线段树)

    要点 显然要滑动修改维护. 像通常的数列next关系一样建边(单调栈预处理),因为贪心所以是树,然后发现增删只会影响区间内的子(or父,看你连边方向行事)节点,于是使用dfs序建线段树. 为了正确地修 ...

  8. Codeforces 1132G(dfs序+线段树)

    题面 传送门 分析 对于每一个数a[i],找到它后面第一个大于它的数a[p],由p向i连边,最终我们就会得到一个森林,且p是i的父亲.为了方便操作,我们再增加一个虚拟节点n+1,把森林变成树. 由于序 ...

  9. Codeforces Testing Round #12 C. Subsequences 树状数组维护DP

    C. Subsequences Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...

随机推荐

  1. 返回xml过长时被nginx截断的解决办法

    返回xml过长时被nginx截断的解决办法 问题描述:通过网页获取数据,数据格式为xml.当xml比较短时,可以正常获取数据.但是xml长度过长时不能正常获取数据,通过观察返回数据的源代码,发现xml ...

  2. 2016NOI冬令营day1

    感冒了!!!: ( 上午听 picks 讲多项式导论(所有内容均不考)只听懂了那个O(n1.585)的多项式乘法算法 : ( 安装好了弹幕!太厉害了(有电脑的都在刷弹幕) :  ( 中午吃的不错 : ...

  3. linux 挂载硬盘 + 对硬盘 分区

    parted命令可以划分单个分区大于2T的GPT格式的分区,也可以划分普通的MBR分区 fdisk命令对于大于2T的分区无法划分,所以用fdisk无法看到parted划分的GPT格式的分区 1. 用 ...

  4. 20145106 《Java程序设计》第10周学习总结

    教材学习内容总结 什么是计算机网络? 计算机网络,是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享 ...

  5. 20145322 《网络对抗》 MSF基础应用1

    20145322何志威 Exp5 MS08_067漏洞测试 实验问答 什么是exploit.payload.encode exploit:通过一个漏洞对程序进行攻击的过程 payload:有具体功能作 ...

  6. 开源项目托管GitHub

    工具:本地HelloWorld源项目 msysgit(Windows) 实验步骤:一.在GitHub新建托管项目 在http://github.com注册账号20159214-sunnan. 完成注册 ...

  7. Bootstrap 使用教程 与jQuery的Ajax方法

    jQuery.ajax(url,[settings]) 更加详细的内容参考    jQuery API 中文在线手册 概述 通过 HTTP 请求加载远程数据. jQuery 底层 AJAX 实现.简单 ...

  8. SxsTrace

    https://troubleshooter.xyz/wiki/fix-the-application-has-failed-to-start-because-the-side-by-side-con ...

  9. C#中dll附加配置文件

    DLL-with-configuration-file带配置文件的dll http://www.codeproject.com/Tips/199441/DLL-with-configuration-f ...

  10. StringUtils类常用的方法讲解

    StringUtils 方法的操作对象是 Java.lang.String 类型的对象,是 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String 为 ...