Given a sequence of N pairs of integers, find the length of the longest increasing subsequence of it.
An increasing sequence A1..An is a sequence such that for every i < j, Ai < Aj.
A subsequence of a sequence is a sequence that 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.
Example
Input: 8 1 3 3 2 1 1 4 5 6 3 9 9 8 7 7 6 Output: 3
题意;求最长的序列LIS,满足i<j,xi<xj ;yi<yj。
思路:裸题,cqd分治,计算每个[L,Mid]区间对[Mid+1,R]区间的贡献。
有两个注意点:
第一:由于时间紧,只有300ms,不能写结构体的排序; 这里借鉴了别人的一维排序(ORZ,强的啊)。
第二:注意规避x1=x2,y1<y2的时候不能用 1去更新2。(和求逆序对那题一样,只有把y坐标的放左边即可)。
#include<bits/stdc++.h> using namespace std; const int maxn=1000010; int p[maxn],a[maxn],b[maxn],dp[maxn],Mx[maxn],tot,ans; bool cmp(int x,int y){ if(a[x]==a[y]) return x>y; return a[x]<a[y]; } void solve(int L,int R) { if(L==R){ dp[L]=max(dp[L],1); return ;} int Mid=(L+R)/2; solve(L,Mid); for(int i=L;i<=R;i++) p[i]=i; sort(p+L,p+R+1,cmp); for(int i=L;i<=R;i++){ if(p[i]<=Mid) for(int j=b[p[i]];j<=tot;j+=(-j)&j) Mx[j]=max(Mx[j],dp[p[i]]); else for(int j=b[p[i]]-1;j;j-=(-j)&j) dp[p[i]]=max(dp[p[i]],Mx[j]+1); } for(int i=L;i<=R;i++) if(p[i]<=Mid) for(int j=b[p[i]];j<=tot;j+=(-j)&j) Mx[j]=0; solve(Mid+1,R); } int main() { int N,i,fcy=0; scanf("%d",&N); for(i=1;i<=N;i++) scanf("%d%d",&a[i],&b[i]),p[i]=b[i]; sort(p+1,p+N+1); tot=unique(p+1,p+N+1)-(p+1); for(i=1;i<=N;i++) b[i]=lower_bound(p+1,p+tot+1,b[i])-p; solve(1,N); for(i=1;i<=N;i++) fcy=max(fcy,dp[i]); printf("%d\n",fcy); return 0; }
原文地址:https://www.cnblogs.com/hua-dong/p/9102612.html