設f[i]表示以i為結尾的最長的合法序列的長度,=號直接維護,<號和>號用兩棵樹狀陣列維護即可,時間複雜度$O(n\log n)$。
#include<cstdio> #define N 1000000 int n,k,i,j,a[N],e[N+1],bl[N+1],bg[N+1],f[N],ans;char s[N]; inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';} inline void read(char&a){while(((a=getchar())!='>')&&(a!='=')&&(a!='<'));} inline void up(int&a,int b){if(a<b)a=b;} inline void add(int*b,int x,int y){for(;x<=N;x+=x&-x)up(b[x],y);} inline int ask(int*b,int x){int t=0;for(;x;x-=x&-x)up(t,b[x]);return t;} int main(){ read(n),read(k); for(i=1;i<=n;i++)read(a[i]); for(i=1;i<k;i++)read(s[i]);read(s[0]); for(i=1;i<=n;i++){ f[i]=e[a[i]]; up(f[i],ask(bl,a[i]-1)); up(f[i],ask(bg,N-a[i])); f[i]++; if(s[f[i]%k]=='=')up(e[a[i]],f[i]); if(s[f[i]%k]=='<')add(bl,a[i],f[i]); if(s[f[i]%k]=='>')add(bg,N-a[i]+1,f[i]); up(ans,f[i]); } return printf("%d",ans),0; }