SPOJ Another Longest Increasing Subsequence Problem

传送门:https://www.spoj.com/problems/LIS2/en/

题意:

给定 N个数对 \((x_i,y_i)\),求最长上升子序列的长度。上升序列定义为满足\((x_i,y_i)\)对i<j 有 \(x_i<x_j\) 且 \(y_i<y_j\)

题解:

一个三维最长链问题

第一维是存位置,第二维存x,第三维存y

注意查询是查询到p[i].z-1然后更新

细节方面和HDU4742是一样的

详情:https://www.cnblogs.com/buerdepepeqi/p/11193426.html

代码:

#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
struct node {
int x, y, z;
} p[maxn];
bool cmpx(node A, node B) {
return A.x < B.x;
}
bool cmpy(node A, node B) {
return A.y < B.y;
}
int lowbit(int x) {
return x & (-x);
}
int n;
int dp[maxn];
int Hash[maxn];
int bit[maxn];
void add(int pos, int val) {
while(pos < n + 2) {
bit[pos] = max(bit[pos], val);
pos += lowbit(pos);
}
}
int sum(int pos) {
int res = 0;
while(pos) {
res = max(res, bit[pos]);
pos -= lowbit(pos);
}
return res;
}
void init(int x) {
for(int i = x; i < n + 2; i += lowbit(i))
bit[i] = 0;
}
void solve(int l, int r) { int mid = (l + r) >> 1;
sort(p + l, p + mid + 1, cmpy);
sort(p + mid + 1, p + r + 1, cmpy);
int j = l;
for(int i = mid + 1; i <= r; i++) {
while(j <= mid && p[j].y < p[i].y) {
add(p[j].z, dp[p[j].x]);
j++;
}
int tmp = sum(p[i].z - 1) + 1;
dp[p[i].x] = max(dp[p[i].x], tmp);
}
for(int i = l; i <= mid; i++) init(p[i].z);
sort(p + mid + 1, p + r + 1, cmpx);
}
void CDQ(int l, int r) {
if(l == r) {
return;
}
int mid = (l + r) >> 1;
CDQ(l, mid);
solve(l, r);
CDQ(mid + 1, r);
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d%d", &p[i].y, &p[i].z);
p[i].x = i;
dp[i] = 1;
Hash[i] = p[i].z;
} sort(Hash + 1, Hash + n + 1);
int cnt = unique(Hash + 1, Hash + n + 1) - Hash - 1;
for(int i = 1; i <= n; i++) {
p[i].z = lower_bound(Hash + 1, Hash + cnt + 1, p[i].z) - Hash;
}
// debug1(n);
CDQ(1, n);
int ans = 0;
for(int i = 1; i <= n; i++) {
ans = max(ans, dp[i]);
}
printf("%d\n", ans); return 0;
}

SPOJ Another Longest Increasing Subsequence Problem 三维最长链的更多相关文章

  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)

    题目链接  LIS2 经典的三维偏序问题. 考虑$cdq$分治. 不过这题的顺序应该是 $cdq(l, mid)$ $solve(l, r)$ $cdq(mid+1, r)$ 因为有个$DP$. #i ...

  4. SPOJ - LIS2 Another Longest Increasing Subsequence Problem

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

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

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

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

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

  7. 【Lintcode】076.Longest Increasing Subsequence

    题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...

  8. LintCode刷题笔记--Longest Increasing Subsequence

    标签: 动态规划 描述: Given a sequence of integers, find the longest increasing subsequence (LIS). You code s ...

  9. [LeetCode] Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

随机推荐

  1. eclipse Some projects cannot be imported because they already exist in the workspace

    archive file 档案文件 删除对应的文件即可

  2. 亚洲唯一,阿里云SLB位列Gartner全球网络负载均衡市场前五

    近日,Gartner发布了最新的全球企业级网络设备市场份额报告“Market Share: Enterprise Network Equipment by Market Segment, Worldw ...

  3. 安装pip3遇到:E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

    安装pip3遇到:E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution). 具 ...

  4. ROS出现“Couldn't find executable named listener below //home/xxx/catkin_ws/src/mypack”问题

    在执行节点时出现了如下图所示的错误: 错误原因是在路径下找不到可执行的节点文件.但事实是已经对工作空间进行了编译,并且在devel /lib/my_serial_node 中已经生成了可执行文件. 如 ...

  5. mysql 连接慢的问题

    现象: 今发现站点訪问数据库变慢,经查,查询数据库非常快,连接数据库比較耗时. 解决的方法: 在mysql的配置文件my.cnf中,在[mysqld]以下加上这个配置就能够了. 附录:[mysqld] ...

  6. IDEA使用中文api鼠标提示的设置

    最近都在用IDEA来练习,发现有的方面确实比eclipse好用,eclipse里面可添加中文的API 提示,对初期的我帮助很大,但是IDEA却没有找到添加的地方,一直以来还以为不支持这个功能,比较遗憾 ...

  7. SDUT-3332&3333_数据结构实验之栈与队列五:下一较大值

    数据结构实验之栈与队列六:下一较大值 Time Limit: 150 ms Memory Limit: 8000 KiB Problem Description 对于包含n(1<=n<=1 ...

  8. "不用谢" 的11种表达

    说Thank you ,机械版的反应you are welcome.虽然没错,但实在太老掉牙,在国外使用率不高: 随性,不足挂齿的小事 Not a problem 别放在心上 Any time 有事随 ...

  9. laravel5.4 发送SMTP邮件

    https://blog.csdn.net/qq_35843527/article/details/77880631 Lumen / Laravel 5.4 使用网易邮箱 SMTP 发送邮件 获取网易 ...

  10. uniapp点击底部tabbar不跳转页面

    一个项目,其设想是这样的,当我进入页面,发现有新版本,提示用户之后,用户点击确定跳转到下载页面. 弹出框要用自己封装的,因为uniapp的弹出框不同的手机上展示的样子不一样,领导的是华为(在这里悄悄吐 ...