# Bzoj5442 [ceoi2018]global warming

## LIS的巧妙转化

Posted by yjjr's blog on September 21, 2018

# 题目

## Sample Input

8 10
7 3 5 12 2 7 3 4


## Sample Output

5


# 题解

• 如果在$[l,r]+num$，不如在$[l,n]+num$
• 如果在$[l,r]-num$，不如在$[1,r]-num$，不如在$(r,n]+num$
• 如果在$[l,r]+1$，不如在$[l,r]+max$，即选定区间加最大值

# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
#define lowbit(x) (x&(-x))
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=2e5+6;
int top,sta[maxn],sum[maxn<<1],n,x,a[maxn],b[maxn<<1],c[maxn],f1[maxn],f2[maxn],ans=0,pos;
void modify(int x,int val){for(int i=x;i<=2*n;i+=lowbit(i))sum[i]=max(sum[i],val);}
inline int getmax(int x){int re=0;for(int i=x;i;i-=lowbit(i))re=max(re,sum[i]);return re;}
int main(){
sort(b+1,b+1+n*2);
rep(i,1,n)a[i]=lower_bound(b+1,b+1+2*n,a[i])-b;
rep(i,1,n)
if(a[i]>sta[top])sta[++top]=a[i],f1[i]=top;
else{pos=lower_bound(sta+1,sta+1+top,a[i])-sta;sta[pos]=a[i],f1[i]=pos;}
top=0;
dep(i,n,1){
int tmp=2*n+1-a[i];
if(tmp>sta[top])sta[++top]=tmp,f2[i]=top;
else{pos=lower_bound(sta+1,sta+1+top,tmp)-sta;sta[pos]=tmp,f2[i]=pos;}
}
modify(a[1],f1[1]);
rep(i,2,n){
int t=lower_bound(b+1,b+1+2*n,c[i]+x-1)-b,post=getmax(t);
if(f2[i]+post>ans)ans=f2[i]+post,pos=t;
modify(a[i],f1[i]);
}
cout<<ans<<endl;
return 0;
}