zoukankan      html  css  js  c++  java
  • Greedy Subsequences CodeForces

    我们从右往左滑动区间, 假设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 '
    '
    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
    ",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;
    }
    
  • 相关阅读:
    试着把.net的GC讲清楚(2)
    试着把.net的GC讲清楚(1)
    【特性】select语句中使用字符串链接获取字段值失败
    twemproxy分片处理原理--剖析twemproxy代码正编
    robot framework的使用说明
    网络故障模拟,cpu高压以及docker中的实现
    我眼中的robot framework
    twemproxyMemcache协议解析探索——剖析twemproxy代码正编补充
    twemproxy代理主干流程——剖析twemproxy代码正编
    Leetcode 617 Merge Two Binary Trees 二叉树
  • 原文地址:https://www.cnblogs.com/uid001/p/10524821.html
Copyright © 2011-2022 走看看