题意:
规定一个子序列为递增的且每一位上的数的下一位恰好是原序列中该数下一个大于他的数。求长度为k的连续区间中最长的子序列的长度是多少
题解:
通过维护一个单调递减的栈来获得一个数后一位数的位置,之后我们可以把得出的这层关系连边形成一个森林方向为大数->小数,并将所有入度为0的与一个须根(n+1)相连。
通过上述操作我们就可以发现,假如加上一个数的贡献,其贡献只会影响其子孙,删掉依然。于是我们联想到线段树来维护每个子树的最大值(其中用树的dfs序来确定其操作范围)之后便是类似滑动窗口的操作。
代码:
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define PB push_back
#define MP make_pair
#define INF 1073741824
#define inf 1152921504606846976
#define pi 3.14159265358979323846
//#pragma comment(linker,"/STACK:10240000,10240000")
const int N=4e6+7,M=2e6;
const long long mod=1e9+7;
inline int read(){int ret=0;char ch=getchar();bool f=1;for(;!isdigit(ch);ch=getchar()) f^=!(ch^'-');for(;isdigit(ch);ch=getchar()) ret=(ret<<1)+(ret<<3)+ch-48;return f?ret:-ret;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}//ÄæÔª
int head[N],NEXT[M],ver[M],tot;void link(int u,int v){ver[++tot]=v;NEXT[tot]=head[u];head[u]=tot;}
int indeg[N],in[N],out[N],lazy[N],a[N];
int ant;
struct node{
int l,r,mid;
int val;
int maxn;
}tree[N];
void dfs(int x){
in[x]=++ant;
for(int i=head[x];i;i=NEXT[i]){
int y=ver[i];
dfs(y);
}
out[x]=ant;
}
void build(int x,int l,int r){
tree[x].l=l;
tree[x].r=r;
lazy[x]=0;
if(l==r){
tree[x].maxn=0;
return;
}
int mi=tree[x].mid=l+r>>1;
build(x<<1,l,mi);
build(x<<1|1,mi+1,r);
tree[x].maxn=max(tree[x<<1].maxn,tree[x<<1|1].maxn);
}
void down(int x){
//if(tree[x*2].l==5&&tree[x*2].r==6) puts("Yes");
if(lazy[x]!=0){
lazy[x<<1]+=lazy[x];
lazy[x<<1|1]+=lazy[x];
tree[x<<1].maxn+=lazy[x];
tree[x<<1|1].maxn+=lazy[x];
lazy[x]=0;
}
}
void change(int x,int l,int r,int num){
if(l>tree[x].r||r<tree[x].l) return;
if(l<=tree[x].l&&r>=tree[x].r){
//if(tree[x].l==5&&tree[x].r==6) puts("YES");
tree[x].maxn+=num;
lazy[x]+=num;
return;
}
else {
down(x);
if(l<=tree[x].mid) change(x<<1,l,r,num);
if(r>tree[x].mid) change(x<<1|1,l,r,num);
tree[x].maxn=max(tree[x<<1].maxn,tree[x<<1|1].maxn);
}
}
int ask(int x,int l,int r){
return tree[x].maxn;
}
int main(){
//freopen("1.txt","r",stdin);
int n,k;
int ant=0;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
stack<int>s;
for(int i=1;i<=n;i++){
if(s.empty()) s.push(i);
else {
while(!s.empty()&&a[s.top()]<a[i]){
link(i,s.top());
indeg[s.top()]++;
s.pop();
}
s.push(i);
}
}
for(int i=1;i<=n;i++){
if(indeg[i]) continue;
link(n+1,i);
}
dfs(n+1);
build(1,in[n+1],out[n+1]);
for(int i=1;i<=n;i++){
//cout<<in[i]<<' '<<out[i]<<endl;
if(i>k){
change(1,in[i-k],out[i-k],-1);
change(1,in[i],out[i],1);
printf(" %d",ask(1,in[n+1],out[n+1]));
}
else {
change(1,in[i],out[i],1);
if(i==k) printf("%d",ask(1,in[n+1],out[n+1]));
}
}
puts("");
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}