SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
2015-11-26 22:26
507 查看
Another Longest Increasing Subsequence Problem
Time Limit: 20 SecMemory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19929Description
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.
Sample Input
8 1 3 3 2 1 1 4 5 6 3 9 9 8 7 7 6
Sample Output
3
HINT
题意求三维偏序最长链
题解:
CDQ分治
树套树会TLE(反正我的会TLE。。。。
代码:
#include<iostream> #include<stdio.h> #include<map> #include<vector> #include<algorithm> using namespace std; const int maxn = 100500+5; inline long long read() { long long x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } struct node { int x,y,z; }p[maxn]; int n; map<int,int> H; vector<int> Q; void Li() { for(int i=1;i<=n;i++) Q.push_back(p[i].z); sort(Q.begin(),Q.end()); for(int i=1;i<=n;i++) p[i].z=lower_bound(Q.begin(),Q.end(),p[i].z)-Q.begin()+1; } bool cmpx(node A,node B) { return A.x<B.x; } bool cmpy(node A,node B) { return A.y<B.y; } int dp[maxn]; int d[maxn]; int lowbit(int x) { return x&(-x); } void updata(int x,int val) { for(int i=x;i<n+2;i+=lowbit(i)) d[i]=max(d[i],val); } int query(int x) { int res = 0; for(int i=x;i;i-=lowbit(i)) res=max(res,d[i]); return res; } void init(int x) { for(int i=x;i<n+2;i+=lowbit(i)) d[i]=0; } void solve(int L,int R){ int m=(L+R)>>1; sort(p+L,p+m+1,cmpy); sort(p+m+1,p+R+1,cmpy); int j=L; for(int i=m+1;i<=R;i++){ for(;j<=m&&p[j].y<p[i].y;j++) updata(p[j].z,dp[p[j].x]); int tmp=query(p[i].z-1)+1; dp[p[i].x]=max(dp[p[i].x],tmp); } for(int i=L;i<=m;i++)init(p[i].z); sort(p+m+1,p+R+1,cmpx); } void CDQ(int L,int R){ if(L==R)return; int m=(L+R)>>1; CDQ(L,m); solve(L,R); CDQ(m+1,R); } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { p[i].y=read(),p[i].z=read(); p[i].x = i; dp[i]=1; } Li(); CDQ(1,n); int Ans = 0; for(int i=1;i<=n;i++) Ans=max(Ans,dp[i]); printf("%d\n",Ans); }
相关文章推荐
- mybatis:java.lang.IllegalArgumentException:Parameter Maps collection does notcontain value for ...
- 读书笔记--其他UI组件
- 108 Which two statements are true about the Automatic Diagnostic Repository (ADR)? (Choose two.) A.
- UIScrollView视差模糊效果
- Android官方文档翻译 五 1.3Building a Simple User Interface
- QUESTION 105 Examine the following RMAN script: RMAN> run { debug on; allocate channel c1 type disk;
- UI错误集
- 基于GUI的简易图像处理系统设计与实现
- UIView设置圆角、边框和阴影
- UITextFieldDelegate协议中各个方法调用情况
- 606第七周周赛 C - Wavio Sequence最长递增递减序列
- Auto Layout 使用心得(五)—— 根据文字、图片自动计算 UITableViewCell 高度
- 深入理解dispatch_queue
- UI-Target-action模式
- 浴室随想——RogueLike随想
- contentWindow,contentDocument, docuemnt ,window,self,top,parent,opener的关系
- crfsuite 安装文档
- leetcode_Range Sum Query 2D - Immutable
- Django POST CSRF verification failed. Request abor
- 进击的KFC:UI(十一)自定义Cell