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 ...
随机推荐
- LeetCode945-使数组唯一的最小增量
问题:使数组唯一的最小增量 给定整数数组 A,每次 move 操作将会选择任意 A[i],并将其递增 1. 返回使 A 中的每个值都是唯一的最少操作次数. 示例 1: 输入:[1,2,2] 输出:1 ...
- set 方法总结整理
#!/usr/bin/env python __author__ = "lrtao2010" #Python 3.7.0 集合常用方法 #集合是无序的,元素不能重复,元素只能是数字 ...
- LeetCode(228) Summary Ranges
题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, g ...
- POJ - 2250 Compromise (LCS打印序列)
题意:给你两个单词序列,求出他们的最长公共子序列. 多组数据输入,单词序列长度<=100,单词长度<=30 因为所有组成LCS的单词都是通过 a[i] == b[j] 更新的. 打印序列的 ...
- 并查集:CDOJ1594-老司机的奇幻漂流 (食物链)
老司机的奇幻漂流 UESTC - 1594 Problem Description 老司机在救出了女票之后,就和她在全世界旅游,有一天,他们来到了一个神奇的小岛上. 这个小岛上有三种动物,他们互相克制 ...
- Linux学习-登录档的轮替(logrotate)
rsyslogd 利用的是 daemon 的方式来启动的, 当有需求的时候立刻就会被执行的,但是 logrotate 却是在规定的时间到了之后才来进行登录档的轮 替, 所以这个 logrotate 程 ...
- [转] vuex最简单、最直白、最全的入门文档
前言 我们经常用element-ui做后台管理系统,经常会遇到父组件给子组件传递数据,下面一个简单的例子,点击按钮,把弹框显示变量数据通过子组件的props属性传递,子组件通过$emit事件监听把数据 ...
- UTV - URL Tag Validation
What`s UTV 1.URL Tag Validation 2.Special format of URL for preventing unauthorized usage and access ...
- linuxlinux0.11源码学习——bootsect.s学习
由于一直想写一个自己的操作系统,网上推荐了<linux内核完全注释>.自学了一个星期,感觉这本书还是很好的,同时写下关于内核代码的理解,如果有什么不对的对方,欢迎大家一起来交流. 在内核引 ...
- js的编码函数
js对文字进行编码,涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent ...