码迷,mamicode.com
首页 > 其他好文 > 详细

SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

时间:2015-11-26 23:07:13      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

Another Longest Increasing Subsequence Problem

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

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 is a sequence such that for every i < jAi < Aj.

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);
}

 

SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

标签:

原文地址:http://www.cnblogs.com/qscqesze/p/4999233.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!