Greedy Subsequences CodeForces - 1132G
我们从右往左滑动区间, 假设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的更多相关文章
- [CF1132G]Greedy Subsequences
[CF1132G]Greedy Subsequences 题目大意: 定义一个序列的最长贪心严格上升子序列为:任意选择第一个元素后,每次选择右侧第一个大于它的元素,直到不能选为止. 给定一个长度为\( ...
- 【CF1132G】Greedy Subsequences(线段树)
[CF1132G]Greedy Subsequences(线段树) 题面 CF 题解 首先发现选完一个数之后选择下一个数一定是确定的. 对于每个数预处理出左侧第一个比他大的数\(L\),那么这个数加入 ...
- Codeforces 1132G Greedy Subsequences [线段树]
洛谷 Codeforces 看到题解那么少就来发一篇吧-- 思路 看完题目一脸懵逼,感觉无从下手. 莫名其妙地想到笛卡尔树,但笛卡尔树好像并没有太大作用. 考虑把笛卡尔树改一下:每个点的父亲设为它的右 ...
- Solution -「CF 1132G」Greedy Subsequences
\(\mathcal{Description}\) Link. 定义 \(\{a\}\) 最长贪心严格上升子序列(LGIS) \(\{b\}\) 为满足以下两点的最长序列: \(\{b\}\) ...
- cf1132G. Greedy Subsequences(线段树)
题意 题目链接 Sol 昨天没想到真是有点可惜了. 我们考虑每个点作为最大值的贡献,首先预处理出每个位置\(i\)左边第一个比他大的数\(l\),显然\([l + 1, i]\)内的数的后继要么是\( ...
- [Codeforces1132G]Greedy Subsequences——线段树+单调栈
题目链接: Codeforces1132G 题目大意:给定一个序列$a$,定义它的最长贪心严格上升子序列为$b$满足若$a_{i}$在$b$中则$a_{i}$之后第一个比它大的也在$b$中.给出一个数 ...
- Codeforces 1132G(关系转化树+dfn+线段树)
要点 显然要滑动修改维护. 像通常的数列next关系一样建边(单调栈预处理),因为贪心所以是树,然后发现增删只会影响区间内的子(or父,看你连边方向行事)节点,于是使用dfs序建线段树. 为了正确地修 ...
- Codeforces 1132G(dfs序+线段树)
题面 传送门 分析 对于每一个数a[i],找到它后面第一个大于它的数a[p],由p向i连边,最终我们就会得到一个森林,且p是i的父亲.为了方便操作,我们再增加一个虚拟节点n+1,把森林变成树. 由于序 ...
- Codeforces Testing Round #12 C. Subsequences 树状数组维护DP
C. Subsequences Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...
随机推荐
- linux常用命令:head 命令
head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 1.命令格式: hea ...
- appium— Android定位webView里面的UI元素
Android SDK中的UIAutomator中本身是不支持网页中的UI元素定位,下面介绍几种常用的定位app内部的网页的UI元素的方法. 一.使用chrome浏览器调试移动端网页 这是使用最多的一 ...
- Nginx服务器之负载均衡策略(6种)
一.关于Nginx的负载均衡 在服务器集群中,Nginx起到一个代理服务器的角色(即反向代理),为了避免单独一个服务器压力过大,将来自用户的请求转发给不同的服务器.详情请查看我的另一篇博客. 二.Ng ...
- 01: Django基础篇
目录:Django其他篇 01:Django基础篇 02:Django进阶篇 03:Django数据库操作--->Model 04: Form 验证用户数据 & 生成html 05:Mo ...
- linux内核分析 1、2章读书笔记
一.linux历史 20世纪60年代,MIT开发分时操作系统(Compatible TIme-Sharing System),支持30台终端访问主机: 1965年,Bell实验室.MIT.GE(通用电 ...
- soapUi在调用过程中日期参数
中间加个T 2012-11-05T16:38:30 相关描述:
- HTML切换页面IE版本
<!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--><!--[if IE]> 所有的IE可识别 <![e ...
- Python3基础 list 索引查看元素
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 从bios启动说起
如果从bios启动说起的话,BIOS去加载bootloader,bootloader去加载操作系统,那么bootloader是怎么找到操作系统的呢?经过一些流程后,它会去找grub:然后通过grub提 ...
- 网络压缩论文整理(network compression)
1. Parameter pruning and sharing 1.1 Quantization and Binarization Compressing deep convolutional ne ...