SPOJ:Another Longest Increasing Subsequence Problem(CDQ分治求三维偏序)

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.

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

时间: 2024-11-02 22:12:00

SPOJ:Another Longest Increasing Subsequence Problem(CDQ分治求三维偏序)的相关文章

hdu5618(cdq分治求三维偏序)

题意:给n(n<=100000)个点,坐标为(xi,yi,zi)(1<=xi,yi,zi<=100000),定义一个点A比一个点B小,当且仅当xA<=xB,yA<=yB,zA<=zB.求对于每个点,有多少个点比它小. 分析: 首先肯定按照x递增顺序排个序 接下来就是每次往平面插入一个点,求这个点左下方已经有多少个点,这可以用二维树状数组来搞,但是很明显会爆空间,不可以接受(当然树套树也是不可以的) 可以考虑对第二维cdq分治 对于一个区间[l,r],先递归区间[l,mi

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

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 incre

SPOJ - LIS2 Another Longest Increasing Subsequence Problem

cdq分治,dp(i)表示以i为结尾的最长LIS,那么dp的递推是依赖于左边的. 因此在分治的时候需要利用左边的子问题来递推右边. (345ms? 区间树TLE /********************************************************* * ------------------ * * author AbyssFish * **********************************************************/ #inclu

LintCode Longest Increasing Subsequence

Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. Clarification What's the definition of longest increasing subsequence? The longest increasing subsequence problem is to find a

LintCode刷题笔记--Longest Increasing Subsequence

动态规划 描述: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. Clarification What's the definition of longest increasing subsequence? The longest increasing subsequence problem is t

[LintCode]76. Longest Increasing Subsequence

Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. Example For [5, 4, 1, 2, 3], the LIS is [1, 2, 3], return 3 For [4, 2, 4, 5, 3, 7], the LIS is [2, 4, 5, 7], return 4 Challenge

300. Longest Increasing Subsequence

Problem statement: Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there

Dynamic Programming | Set 3 (Longest Increasing Subsequence)

在 Dynamic Programming | Set 1 (Overlapping Subproblems Property) 和 Dynamic Programming | Set 2 (Optimal Substructure Property) 中我们已经讨论了重叠子问题和最优子结构性质,现在我们来看一个可以使用动态规划来解决的问题:最长上升子序列(Longest Increasing Subsequence(LIS)). 最长上升子序列问题,致力于在一个给定的序列中找到一个最长的子序列

[Coding Made Simple] Longest Increasing Subsequence

Given an array, find the longest increasing subsequence in this array. The same problem link. [LintCode] Longest Increasing Subsequence