zoukankan      html  css  js  c++  java
  • 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 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
    ",Ans);
    }
  • 相关阅读:
    交流课件
    ARC127F ±AB
    CF1566F xor-quiz
    JOISC 2016 Day 1 棋盘游戏
    dev分支和release是什么
    drf 笔记
    drf 序列化的写法 总结
    Linux系统编程
    C/C++ 网络编程
    C++提高编程
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4999233.html
Copyright © 2011-2022 走看看