【Luogu】 P3928 SAC E#1 - 一道简单题 Sequence2
【题目】洛谷10月月赛R1 提高组
【算法】递推DP+树状数组
【题解】列出DP递推方程,然后用树状数组维护前后缀和。
#include<cstdio> #include<cstring> #include<cctype> #include<cmath> #include<algorithm> #define lowbit(x) (x&-x) #define ll long long using namespace std; int read(){ char c;int s=0,t=1; while(!isdigit(c=getchar()))if(c=='-')t=-1; do{s=s*10+c-'0';}while(isdigit(c=getchar())); return s*t; } int min(int a,int b){return a<b?a:b;} int max(int a,int b){return a<b?b:a;} int abs(int x){return x>0?x:-x;} //void insert(int u,int v){tot++;e[tot].v=v;e[tot].from=first[u];first[u]=tot;} /*------------------------------------------------------------*/ const int inf=0x3f3f3f3f,maxn=300010; int n,a[maxn][3],f[maxn][4],ans,tot,c[5][maxn],b[maxn]; void modify(int d,int x,int k){if(d&1)x=tot-x+1;for(int i=x;i<=tot;i+=lowbit(i))c[d][i]=max(c[d][i],k);} int ask(int d,int x){if(d&1)x=tot-x+1;int as=0;for(int i=x;i>=1;i-=lowbit(i))as=max(as,c[d][i]);return as;} int main(){ n=read();tot=0; for(int i=1;i<=n;i++)a[i][0]=read(),f[i][0]=1,b[++tot]=a[i][0]; for(int i=1;i<=n;i++)a[i][1]=read(),f[i][1]=1,b[++tot]=a[i][1]; for(int i=1;i<=n;i++)a[i][2]=read(),f[i][2]=f[i][3]=1,b[++tot]=a[i][2]; sort(b+1,b+tot+1); tot=unique(b+1,b+tot+1)-b-1; for(int i=1;i<=n;i++)a[i][0]=lower_bound(b+1,b+tot+1,a[i][0])-b; for(int i=1;i<=n;i++)a[i][1]=lower_bound(b+1,b+tot+1,a[i][1])-b; for(int i=1;i<=n;i++)a[i][2]=lower_bound(b+1,b+tot+1,a[i][2])-b; modify(0,a[1][0],1);modify(0,a[1][1],1);modify(0,a[1][2],1); modify(1,a[1][0],1);modify(1,a[1][1],1);modify(1,a[1][2],1); modify(2,a[1][0],1);modify(2,a[1][1],1);modify(2,a[1][2],1); modify(3,a[1][0],1);modify(3,a[1][1],1);modify(3,a[1][2],1); for(int i=2;i<=n;i++){ f[i][0]=max(f[i][0],ask(0,a[i][0])+1);ans=max(ans,f[i][0]); f[i][1]=max(f[i][1],ask(1,a[i][1])+1);ans=max(ans,f[i][1]); f[i][2]=max(f[i][2],ask(2,a[i][2])+1);ans=max(ans,f[i][2]); f[i][3]=max(f[i][3],ask(3,a[i][2])+1);ans=max(ans,f[i][3]); modify(0,a[i][0],f[i][0]);modify(0,a[i][1],f[i][1]);modify(0,a[i][2],max(f[i][2],f[i][3])); modify(1,a[i][0],f[i][0]);modify(1,a[i][1],f[i][1]);modify(1,a[i][2],max(f[i][2],f[i][3])); modify(2,a[i][0],f[i][0]);modify(2,a[i][1],f[i][1]);modify(2,a[i][2],f[i][2]); modify(3,a[i][0],f[i][0]);modify(3,a[i][1],f[i][1]);modify(3,a[i][2],f[i][3]);// } printf("%d",ans); return 0; }