题目链接  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. OOP中常用到的函数

    学习地址: http://www.jikexueyuan.com/course/2420.html 判断类是否存在 class_exists() 得到类或者对象中的成员方法组成的数组 get_clas ...

  2. POJ 1791 Parallelogram Counting(求平行四边形数量)

    Description There are n distinct points in the plane, given by their integer coordinates. Find the n ...

  3. 线段树:CDOJ1597-An easy problem C(区间更新的线段树)

    An easy problem C Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...

  4. Centos启动时停止在登录界面但不显示登录信息(一直在转圈)

    进入单用户模式  执行 iscsiadm -m node -o delete,然后reboot

  5. STVP烧录教程

    可以运行独立的烧录软件ST Visual Programmer (STVP)进行STM8芯片烧录.运行“开始”->ST Toolset->Development Tools -> S ...

  6. rocketmq源码分析1-benchmark学习

    benchmark 分析 组成部分 三个java类,都含有main方法,可选的传递一些参数,诸如测试线程数量,消息体积大小.三个类分别用于测试普通生产者,事务生产者,消费者.生产者 默认64个测试线程 ...

  7. python - 自动化测试框架 - logger

    # -*- coding:utf-8 -*- '''@project: Voctest@author: Jimmy@file: log.py@ide: PyCharm Community Editio ...

  8. day04_01 知识回顾、算术运算符

    ","和"+"的区别 除法运算,整除//,别名"地板除" 取余数 2**10 2的10次方 指数运算 指数运算符优先级要比乘法要高,所以先算 ...

  9. x86 保护模式 十 分页管理机制

    x86   保护模式  十  分页管理机制 8.386开始支持分页管理机制 段机制实现虚拟地址到线性地址的转换,分页机制实现线性地址到物理地址的转换.如果不启用分页,那么线性就是物理地址 一  分页管 ...

  10. Django Rest Framework 教程及API向导

    Django Rest Framework 教程及API向导. 一.请求(Request)REST_FRAMEWORK 中的 Request 扩展了标准的HttpRequest,为 REST_FRAM ...