Another longest increasing subsequence problem
Time Limit:20 Sec
Memory limit:256 MB
Topic Connection
http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19929
Description
Given a sequence of N pairs of integers, find the length of the longest increasing subsequence of it.
An increasing sequence A1. An was a sequence such that for every i < J, Ai < Aj.
A subsequence of a sequence is a sequence this appears in the same relative order, but not necessarily contiguous .
A pair of integers (x1, y1) is less than (x2, y2) iff x1 < x2 and y1 < y2.
Input
The first line of input contains an integer N (2≤n≤100000).
The following n lines consist of n pairs of integers (xi, Yi) ( -109≤ xi, Yi ≤109).
Output
The output contains an integer:the length of the longest increasing subsequence of the given sequence.
Sample Input
81 33 21 14 56 39 98 77 6
Sample Output
3
HINT
Test instructions
Finding the longest chain of three-dimensional partial order
Exercises
CDQ Divided treatment
Tree sets the tree will tle (anyway my will tle ....)
Code:
#include <iostream>#include<stdio.h>#include<map>#include<vector>#include<algorithm>using namespacestd;Const intMAXN =100500+5; inlineLong LongRead () {Long Longx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}structnode{intx, Y, Z;} P[MAXN];intN;map<int,int>H;vector<int>Q;voidLi () { for(intI=1; i<=n;i++) Q.push_back (P[I].Z); Sort (Q.begin (), Q.end ()); for(intI=1; i<=n;i++) P[i].z=lower_bound (Q.begin (), Q.end (), p[i].z)-q.begin () +1;}BOOLcmpx (node A,node B) {returna.x<b.x;}BOOLcmpy (node A,node B) {returna.y<b.y;}intDP[MAXN];intD[MAXN];intLowbit (intx) { returnx& (-x);}voidUpdata (intXintval) { for(inti=x;i<n+2; i+=lowbit (i)) D[i]=Max (d[i],val);}intQueryintx) { intres =0; for(inti=x;i;i-=lowbit (i)) Res=Max (res,d[i]); returnRes;}voidInitintx) { for(inti=x;i<n+2; i+=lowbit (i)) D[i]=0;}voidSolveintLintR) { intM= (l+r) >>1; Sort (P+l,p+m+1, Cmpy); Sort (P+m+1, p+r+1, Cmpy); intj=m; for(inti=m+1; i<=r;i++){ for(; j<=m&&p[j].y<p[i].y;j++) Updata (p[j].z,dp[p[j].x]); intTmp=query (p[i].z-1)+1; Dp[p[i].x]=Max (dp[p[i].x],tmp); } for(inti=l;i<=m;i++) init (P[I].Z); Sort (P+m+1, p+r+1, cmpx);}voidCDQ (intLintR) { if(L==R)return; intM= (l+r) >>1; CDQ (L,M); Solve (L,R); CDQ (M+1, R);}intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++) {p[i].y=read (), p[i].z=read (); p[i].x=i; Dp[i]=1; } Li (); CDQ (1, N); intAns =0; for(intI=1; i<=n;i++) Ans=Max (ans,dp[i]); printf ("%d\n", Ans);}
Spoj LIS2 Another longest increasing subsequence problem three-dimensional partial order longest chain CDQ division