题目链接  LIS2

经典的三维偏序问题。

考虑$cdq$分治。

不过这题的顺序应该是

$cdq(l, mid)$

$solve(l, r)$

$cdq(mid+1, r)$

因为有个$DP$。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define MP make_pair
#define fi first
#define se second typedef long long LL; const int N = 1e5 + 10; struct node{
int x, y, z;
int num;
void scan() { scanf("%d%d", &y, &z);}
void print() { printf("%d %d %d\n", x, y, z);}
friend bool operator < (const node &a, const node &b){
return a.y == b.y ? a.z < b.z : a.y < b.y;
}
} p[N], q[N]; int a[N], b[N];
int n;
int cnt;
int c[N];
int ans; void update(int x, int val){
for (; x <= n; x += x & -x) c[x] = max(c[x], val);
} int query(int x){
int ret = 0;
for (; x ; x -= x & -x) ret = max(ret, c[x]);
return ret;
} void recover(int x){
for (; x <= n; x += x & -x) c[x] = 0;
} bool cmp(const node &a, const node &b){
return a.x < b.x;
} void cdq(int l, int r){
if (l == r) return;
int mid = (l + r) >> 1;
cdq(l, mid);
sort(p + l, p + mid + 1);
sort(p + mid + 1, p + r + 1);
int j = l;
for (int i = mid + 1; i <= r; ++i){
for (; j <= mid && p[j].y < p[i].y; ++j){
update(p[j].z, p[j].num);
} p[i].num = max(p[i].num, query(p[i].z - 1) + 1);
} rep(i, l, mid) recover(p[i].z);
sort(p + mid + 1, p + r + 1, cmp);
cdq(mid + 1, r); } int main(){ scanf("%d", &n);
rep(i, 1, n) p[i].scan();
rep(i, 1, n) a[i] = p[i].y;
rep(i, 1, n) p[i].num = 1; sort(a + 1, a + n + 1);
cnt = unique(a + 1, a + n + 1) - a - 1;
rep(i, 1, n) p[i].y = lower_bound(a + 1, a + cnt + 1, p[i].y) - a; rep(i, 1, n) a[i] = p[i].z;
sort(a + 1, a + n + 1);
cnt = unique(a + 1, a + n + 1) - a - 1;
rep(i, 1, n) p[i].z = lower_bound(a + 1, a + cnt + 1, p[i].z) - a; rep(i, 1, n) p[i].x = i; cdq(1, n);
ans = 0;
rep(i, 1, n) ans = max(ans, p[i].num);
printf("%d\n", ans);
return 0;
}

SPOJ LIS2 - Another Longest Increasing Subsequence Problem(CDQ分治优化DP)的更多相关文章

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

    Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...

  2. 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 ...

  3. SPOJ - LIS2 Another Longest Increasing Subsequence Problem

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

  4. [BZOJ2225][SPOJ2371]LIS2 - Another Longest Increasing Subsequence Problem:CDQ分治+树状数组+DP

    分析 这回试了一下三级标题,不知道效果怎么样? 回到正题,二维最长上升子序列......嗯,我会树套树. 考虑\(CDQ\)分治,算法流程: 先递归进入左子区间. 将左,右子区间按\(x\)排序. 归 ...

  5. SPOJ Another Longest Increasing Subsequence Problem 三维最长链

    SPOJ Another Longest Increasing Subsequence Problem 传送门:https://www.spoj.com/problems/LIS2/en/ 题意: 给 ...

  6. 洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP

    洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP 题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他. 玩具上有一个数列,数列中某些项的值可能会 ...

  7. luogu4093 序列 (cdq分治优化dp)

    设f[i]是以i位置为结尾的最长满足条件子序列的长度 那么j能转移到i的条件是,$j<i , max[j]<=a[i] , a[j]<=min[i]$,其中max和min表示这个位置 ...

  8. BZOJ1492 货币兑换 CDQ分治优化DP

    1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec  Memory Limit: 64 MB Description 小Y最近在一家金券交易所工作.该金券交易所只发行交 ...

  9. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

随机推荐

  1. 如何使用jmeter做接口测试

    1.传参:key=value形式 2.传参:json格式 3.jmeter上传文件 4.jmeter传cookie 或者使用 HTTP Cookie管理器

  2. tempfs详解

    致因 在平常工作中,我们经常需要查看Linux服务器磁盘挂载使用情况,可以使用df命令,不知大家注意到没有,我们使用此命令除了会查看到系统盘以及数据盘挂载情况,还会看到一个tmpfs也在挂载. [ro ...

  3. destoon后台权限-不给客户创始人权限并屏蔽部分功能

    1.根目录下后台入口文件admin.php $_founder = $CFG['founderid'] == $_userid ? $_userid : 0;  //  $CFG['founderid ...

  4. Ubuntu 16.04中安装谷歌Chrome浏览器

    1.进入 Ubuntu 16.04 桌面,按下 Ctrl + Alt + t 键盘组合键,启动终端. 2.在终端中,输入以下命令: sudo wget https://repo.fdzh.org/ch ...

  5. hihocoder1175 拓扑排序2

    #1175 : 拓扑排序·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho所在学校的校园网被黑客入侵并投放了病毒.这事在校内BBS上立刻引起了大家的讨论 ...

  6. Linux实现内容分发的主备模式的智能DNS

    BIND实现智能DNS的原理是通过view的方式,首先判断客户请求的来源,然后返回不同的IP 规划:为za.com域进行智能解析 分2个网段,192.168.1.0/24网段的请求解析到192.168 ...

  7. CI 分页类的使用

    分页本身很简单,无非就是一个 [limit $offset, $length] 的过程. $length 是每页显示的数据量,这个是固定的.要确定的就只有 $offset了. 在CI中的分页类同样要依 ...

  8. python算法-汉诺塔问题

    汉诺塔问题   初始状态: 思考:当盘子的个数是3的时候,大家写出移动顺序 移动的步骤: 3个盘子,从a到c 1.前面两个盘子,从a到b 1)把前面一个盘子,从a到c a->c 2)把第二个盘子 ...

  9. [uiautomator篇][10] uiautomator进阶

    http://coderlin.coding.me/2016/07/02/Android-%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%87%AA%E5%8A%A8%E5%8C%96% ...

  10. 清除Jquery动画的队列

    当我们在写页面效果时,有时希望当鼠标放到某个元素上,这时会有动态的效果,当鼠标移出时效果会消失.但实际中,如果快速的用鼠标指向元素并移出,反复几次.即便鼠标不再指向这个元素,但这个元素会不停的重复着动 ...