SPOJ:Another Longest Increasing Subsequence Problem(CDQ分治求三维偏序)
2018-05-28 22:16
537 查看
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.
The following N lines consist of N pairs of integers (xi, yi) (-109 ≤ xi, yi ≤ 109).
题意;求最长的序列LIS,满足i<j,xi<xj ;yi<yj。
思路:裸题,cqd分治,计算每个[L,Mid]区间对[Mid+1,R]区间的贡献。
有两个注意点:
第一:由于时间紧,只有300ms,不能写结构体的排序; 这里借鉴了别人的一维排序(ORZ,强的啊)。
第二:注意规避x1=x2,y1<y2的时候不能用 1去更新2。(和求逆序对那题一样,只有把y坐标的放左边即可)。
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.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; }
相关文章推荐
- SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
- SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
- SPOJ LIS2 (Another Longest Increasing Subsequence Problem) CDQ求三维偏序+离散化+LIS
- BZOJ_2225_[Spoj 2371]Another Longest Increasing_CDQ 分治+树状数组
- SPOJ - LIS2 Another Longest Increasing Subsequence Problem
- BZOJ 2225 [Spoj 2371]Another Longest Increasing(CDQ分治)
- BZOJ 3262 陌上花开、HDU 5618 Jam's problem again(三维偏序、cdq分治 + BIT)
- BZOJ 2225: [Spoj 2371]Another Longest Increasing CDQ
- CDQ分治--模板 BZOJ 3262--陌上花开【三维偏序】
- 洛谷P3810(三维偏序,CDQ分治果题)
- BZOJ.1935.[SHOI2007]Tree园丁的烦恼(CDQ分治 三维偏序)
- [bzoj] 3263 陌上花开 洛谷 P3810 三维偏序|| CDQ分治 && CDQ分治讲解
- (SPOJ - ELIS )Easy Longest Increasing Subsequence(DP)
- hdu5618(cdq分治求三维偏序)
- BZOJ - 3262 陌上花开 CDQ分治 三维偏序
- bzoj3262陌上花开 cdq分治 三维偏序
- BZOJ.3262.陌上花开([模板]CDQ分治 三维偏序)
- 【BZOJ3295】动态逆序对(CQOI2011)-CDQ分治:三维偏序
- 【模板】三维偏序-cdq分治
- HDU 5618 CDQ分治(三维偏序)