zoukankan      html  css  js  c++  java
  • 【CF1132G】Greedy Subsequences(线段树)

    【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;
    }
    
  • 相关阅读:
    Linux启动新进程的几种方法及比较[转]
    部署WEB应用的三种方式[转]
    HTML form label
    其他对象的表单
    Input对象2(貌似是独立标签)
    通过表单展示不一样的页面(input对象)
    神奇的表单
    有效地管理页面布局
    css新奇技术及其未来发展
    进一步讨论页面布局的方法
  • 原文地址:https://www.cnblogs.com/cjyyb/p/10657428.html
Copyright © 2011-2022 走看看