1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<climits> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std;
inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; }
const int maxn=100005;
struct Segment_Tree { struct Tree { int left,right,size; Tree(int l=0,int r=0,int s=0):left(l),right(r),size(s) {} } tree[maxn<<2]; #define ls index<<1 #define rs index<<1|1 void build(int index,int Left,int Right) { tree[index]=Tree(Left,Right,Right-Left+1); if(Left==Right)return; int mid=(Left+Right)>>1; build(ls,Left,mid); build(rs,mid+1,Right); } int query(int index,int v) { tree[index].size--; if(tree[index].left==tree[index].right)return tree[index].left; if(tree[ls].size>=v)return query(ls,v); else return query(rs,v-tree[ls].size); } } st;
int n,a[maxn],b[maxn],c[maxn],id[maxn];
struct BIT { int c[maxn]; #define lowbit(x) x&(-x) void add(int x,int v) { for(int i=x; i<=n; i+=lowbit(i))c[i]=max(c[i],v); } int query(int x) { int ans=0; for(int i=x; i>=1; i-=lowbit(i))ans=max(ans,c[i]); return ans; } } bit;
bool cmp(int x,int y) { return c[x]<c[y]; }
int main() { n=Get_Int(); st.build(1,1,n); for(int i=1; i<=n; i++)a[i]=Get_Int(); for(int i=1; i<=n; i++) { c[i]=Get_Int(); id[i]=i; } for(int i=n; i>=1; i--)b[i]=st.query(1,a[i]); sort(id+1,id+n+1,cmp); for(int i=1; i<=n; i++)bit.add(b[id[i]],bit.query(b[id[i]])+1); printf("%d\n",bit.query(n)); return 0; }
|