SPOJ LIS2 - Another Longest Increasing Subsequence Problem(CDQ分治优化DP)
题目链接 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)的更多相关文章
- SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...
- 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 ...
- SPOJ - LIS2 Another Longest Increasing Subsequence Problem
cdq分治,dp(i)表示以i为结尾的最长LIS,那么dp的递推是依赖于左边的. 因此在分治的时候需要利用左边的子问题来递推右边. (345ms? 区间树TLE /****************** ...
- [BZOJ2225][SPOJ2371]LIS2 - Another Longest Increasing Subsequence Problem:CDQ分治+树状数组+DP
分析 这回试了一下三级标题,不知道效果怎么样? 回到正题,二维最长上升子序列......嗯,我会树套树. 考虑\(CDQ\)分治,算法流程: 先递归进入左子区间. 将左,右子区间按\(x\)排序. 归 ...
- SPOJ Another Longest Increasing Subsequence Problem 三维最长链
SPOJ Another Longest Increasing Subsequence Problem 传送门:https://www.spoj.com/problems/LIS2/en/ 题意: 给 ...
- 洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP
洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP 题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他. 玩具上有一个数列,数列中某些项的值可能会 ...
- luogu4093 序列 (cdq分治优化dp)
设f[i]是以i位置为结尾的最长满足条件子序列的长度 那么j能转移到i的条件是,$j<i , max[j]<=a[i] , a[j]<=min[i]$,其中max和min表示这个位置 ...
- BZOJ1492 货币兑换 CDQ分治优化DP
1492: [NOI2007]货币兑换Cash Time Limit: 5 Sec Memory Limit: 64 MB Description 小Y最近在一家金券交易所工作.该金券交易所只发行交 ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
随机推荐
- PHP 代码优化建议
1.尽量静态化: 如果一个方法能被静态,那就声明它为静态的,速度可提高1/4,甚至我测试的时候,这个提高了近三倍.当然了,这个测试方法需要在十万级以上次执行,效果才明显.其实静态方法和非静态方法的效率 ...
- leetcode-27-exercise_bit maniputation
461. Hamming Distance 解题思路: 把两个数的每一位和1比较,如果结果不同说明这两位不同.要比较32次. int hammingDistance(int x, int y) { i ...
- leetcode-26-exercise_linked-list
141. Linked List Cycle Given a linked list, determine if it has a cycle in it. 解题思路: 需要检查before和afte ...
- The 2018 ACM-ICPC Chinese Collegiate Programming Contest Maximum Element In A Stack
//利用二维数组模拟 #include <iostream> #include <cstdio> #include <cstring> #include <s ...
- 算法学习记录-查找——二叉排序树(Binary Sort Tree)
二叉排序树 也称为 二叉查找数. 它具有以下性质: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值. 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值. 它的左.右子树也分别 ...
- UVa 12235 状压DP Help Bubu
题解戳这 一开始没看懂题解,后来想明白以后,d(i, j, s, x)是考虑第i本书的时候,前面已经拿走了j本书,剩下的书的种类的二进制状态为s,剩下的最后一本书的编号为x,所能得到的最小混乱度. 这 ...
- web安全测试---跨站点脚本测试
1.1 跨站脚本测试 1.1.1 GET方式跨站脚本测试 编号 SEC_Web_XSS_01 测试用例名称 GET方式跨站脚本测试 测试目的 由于跨站脚本会导致会话被劫持.敏感 ...
- 慢查询阻塞了xtrabackup进而阻塞以后的sql导致的系统瘫痪问题
收到开发反应一库的sql频繁超时,系统几乎瘫痪,无法执行任何操作,我登上库先查看到当前的线程,发现有大量的线程状态是 Waiting for table flush 查看当前的事务 从昨天开始执行,到 ...
- Linux Programming之MySQL
实验环境:Ubuntu13.04 在此之前有过一段使用MySQL数据库的经历,在Windows平台下使用GUI(当时是使用HeidiSQL和Workbench来管理数据库),并且有过使用Python中 ...
- ACM程序设计选修课——1040: Alex and Asd fight for two pieces of cake(YY+GCD)
1040: Alex and Asd fight for two pieces of cake Time Limit: 1 Sec Memory Limit: 128 MB Submit: 27 ...