zoukankan      html  css  js  c++  java
  • 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
    ",fcy);
        return 0;
    }
  • 相关阅读:
    前端3 浮动布局,固定定位,绝对定位,相对定位
    前端2 字体|文本属性样式, 高级选择器
    前端1.概念
    前端,基础选择器,嵌套关系.display属性,盒模型
    数据库之索引
    数据库之进阶(视图,事务,存储过程)
    数据库,多表数据
    数据库之表的使用
    数据的演化(数据仓库的发展史)
    HDFS退出安全模式
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9102612.html
Copyright © 2011-2022 走看看