SPOJ - LIS2 Another Longest Increasing Subsequence Problem
cdq分治,dp(i)表示以i为结尾的最长LIS,那么dp的递推是依赖于左边的。
因此在分治的时候需要利用左边的子问题来递推右边。
(345ms? 区间树TLE
/*********************************************************
* ------------------ *
* author AbyssFish *
**********************************************************/
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<numeric>
using namespace std; const int MAX_N = +; int dp[MAX_N];
int x[MAX_N], y[MAX_N];
int ys[MAX_N];
int id[MAX_N];
int N; int ns; int *cur;
bool cmp(int a,int b)
{
return cur[a] < cur[b] || (cur[a] == cur[b] && a > b);//这是为了保证严格的单调性
} int compress(int *r, int *dat, int *a, int n)
{
for(int i = ; i < n; i++){
r[i] = i;
}
cur = dat;
sort(r,r+n,cmp);
a[r[]] = ;
for(int i = ; i < n; i++){
int k = r[i], p = r[i-];
a[k] = dat[k] == dat[p]?a[p]:a[p]+;
}
return a[r[n-]];
} int C[MAX_N]; void add(int yi,int d)
{
while(yi <= ns){
C[yi] = max(C[yi],d);
yi += yi&-yi;
}
} int mx_pfx(int yi)
{
int re = ;
while(yi > ){
re = max(C[yi],re);
yi &= yi-;
}
return re;
} void clr(int yi)
{
while(yi <= ns){
C[yi] = ;
yi += yi&-yi;
}
} void dv(int l, int r)
{
if(r-l <= ){
dp[l]++;
}
else {
int md = (l+r)>>;
dv(l,md); for(int i = l; i < r; i++) id[i] = i;
sort(id+l,id+r,cmp); //x维度 for(int i = l; i < r; i++){
int k = id[i];
if(k < md){ //position 维度
add(ys[k],dp[k]); //BIT下标是 y维度
}
else {
//查询位置前保证了BIT里的元素, 位置md之前,x严格小于待查元素
dp[k] = max(dp[k], mx_pfx(ys[k]-));//y严格小于待查元素的最大dp值
}
} for(int i = l; i < r; i++){
if(id[i] < md)
clr(ys[id[i]]);
}
dv(md,r);
}
} void solve()
{
scanf("%d",&N);
for(int i = ; i < N; i++){
scanf("%d%d",x+i,y+i);
}
ns = compress(id,y,ys,N);
cur = x;
dv(,N);
printf("%d\n",*max_element(dp,dp+N));
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
solve();
return ;
}
SPOJ - LIS2 Another Longest Increasing Subsequence Problem的更多相关文章
- SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...
- SPOJ LIS2 - Another Longest Increasing Subsequence Problem(CDQ分治优化DP)
题目链接 LIS2 经典的三维偏序问题. 考虑$cdq$分治. 不过这题的顺序应该是 $cdq(l, mid)$ $solve(l, r)$ $cdq(mid+1, r)$ 因为有个$DP$. #i ...
- [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/ 题意: 给 ...
- 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 ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- 【Lintcode】076.Longest Increasing Subsequence
题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...
- LintCode刷题笔记--Longest Increasing Subsequence
标签: 动态规划 描述: Given a sequence of integers, find the longest increasing subsequence (LIS). You code s ...
- [LeetCode] Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
随机推荐
- 工具类_GsonUtils
import java.lang.reflect.Type; import com.google.gson.Gson; /** * Gson工具类 2015-02-01<br/> * 1, ...
- 回滚revert和reset区别
分享请标明来自: https://www.css3.io/hui-gun.html 背景 git是一个庞大的工具,我们要开始扫盲一些常用的命令.回滚代码在项目中必然会遇到,下面我们介绍在git中如何回 ...
- 10.Web组件复用
1.静态包含(一个指令) 在软件工程中构建可复用组件可极大的提高软件生产效率.增强系统的可维护性HTML标记对于一个web应用系统中的页面都是通用的,比如公司标志.版权声明.导航菜单JSP中引用的we ...
- python 元组拾遗
python 元组拾遗 python 语法糖 def printall(*args): return args 一个以 * 开头的参数将参数聚集为一个元组. printall函数可以接收任意个数的 ...
- ubuntu下找不到eth0
1.,查找不到eth0 2.修改/etc/network/interface 发现没有eth0网卡信息 添加如下 autho eth0 iface eth0 inet dhcp 执行 sudo /et ...
- mysql 死锁解决办法
查询表的时候,发现一圈圈转啊转,就是不出来数据,猜测表被锁住 解决办法 : mysql> show processlist ; mysql> kill 4; 说明 : 4为 i ...
- Boxes in a Line UVA - 12657 (双向链表)
题目链接:https://vjudge.net/problem/UVA-12657 题目大意:输入n,m 代表有n个盒子 每个盒子最开始按1~n排成一行 m个操作, 1 x y :把盒子x放到y ...
- WSGI学习系列Paste
Paste has been under development for a while, and has lots of code in it. The code is largely decoup ...
- jemeter+badboy录制脚本
Jmeter 是一个非常流行的性能测试工具,虽然与LoadRunner相比有很多不足,比如:它结果分析能力没有LoadRunner详细:很它的优点也有很多: l 开源,他是一款开源的免费软 ...
- 如何解决css和js的{}与smarty定界符冲突的问题
当输入url地址后网页出现:Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error i ...