hdu4742
题意:给定3维的n(<=100000)个点,求最长不下降子序列长度(对于1和2两个点,2可以放在1后面,x1<=x2,y1<=y2,z1<=z2 ),并求出有多少种方案。
思路:裸的cdq分治。。
首先可以先对x排序,就降成二维了。。
定义solve(l,r)为处理[l,r]的区间
那么solve(l,r)为如下:{
solve(l, mid)
对于l,r的点按照y关键字排序
然后从左到右(y增大方向)扫描,对于(l, mid)的点插入将值插入z离散化的数据结构里维护,
对于(mid+1,r)的点直接查询数据结果,更改结果
清空数据结构。
solve(mid+1, r)
}
大致就这样,可以做到nlog^2n的复杂度。。数据结构可以选用bit或者线段树。。不过大家都选用bit吧。。
cdq果然强大,还需慢慢体会呀。。
/*
* Author: Yzcstc
* Created Time: 2014年10月03日 星期五 22时57分13秒
* File Name: hdu4742.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <utility>
#define M0(x) memset(x, 0, sizeof(x))
#define MP make_pair
#define PB push_back
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define Inf 0x3fffffff
#define eps 1e-8
#define pi acos(-1)
#define maxn 120000
#define X first
#define Y second
using namespace std;
typedef pair<int, int> pii;
struct point{
int x, y, z,id;
void input(){
scanf("%d%d%d", &x, &y, &z);
}
bool operator<(const point& p) const{
if (x < p.x) return true;
if (x == p.x && y < p.y) return true;
if (x == p.x && y == p.y && z < p.z) return true;
return false;
}
} p[maxn], pt[maxn];
int t[maxn], n, m;
pii dp[maxn], v[maxn], zero(, ); void init(){
scanf("%d", &n);
for (int i = ; i <= n; ++i)
p[i].input(), t[i-] = p[i].z;
sort(p + , p + + n);
sort(t, t + n);
m = unique(t, t + n) - t;
// cout << m << endl;
for (int i = ; i <= n; ++i)
p[i].z = lower_bound(t, t + m, p[i].z) - t + ;
// repf(i, 1, n) printf("%d\n" ,p[i].z);
for (int i = ; i <= n; ++i)
p[i].id = i;
} /** BIT **/
inline int lowbit(const int& x){
return x & (-x);
} inline void update(pii &a, const pii &b){
if (a.X < b.X) a = b;
else if (a.X == b.X) a.Y += b.Y;
} void modify(int x, const pii& val){
for ( ; x <= m; x += lowbit(x))
update(v[x], val);
} pii query(int x){
pii ret(, );
for ( ; x > ; x -= lowbit(x))
update(ret, v[x]);
return ret;
} void recover(int x){
for (; x <= m; x += lowbit(x))
v[x] = zero;
}
/** BIT-end **/
/** Divide and conquer**/
pii tmp;
void solve(const int& l,const int& r){
if (l == r) return;
int mid = (l + r) >> ;
solve(l, mid);
int sz = ;
for (int i = l; i <= r; ++i)
pt[sz] = p[i], pt[sz++].x = ;
sort(pt, pt+sz);
for (int i = ; i < sz; ++i)
if (pt[i].id <= mid)
modify(pt[i].z, dp[pt[i].id]);
else
tmp = query(pt[i].z), ++tmp.X, update(dp[pt[i].id], tmp);
for (int i = ; i < sz; ++i)
if (pt[i].id <= mid)
recover(pt[i].z);
solve(mid + , r);
} void solve(){
for (int i = ; i <= n; ++i)
dp[i].X = dp[i].Y = ;
solve(, n);
pii ans(, );
for (int i = ; i <= n; ++i)
update(ans, dp[i]); // printf("i = %d: %d %d\n", i, dp[i].first, dp[i].second);
printf("%d %d\n", ans.X, ans.Y);
} int main(){
// freopen("a.in", "r", stdin);
// freopen("a.out", "w", stdout);
int cas;
scanf("%d", &cas);
while (cas--){
init();
solve();
}
return ;
}
hdu4742的更多相关文章
- HDU4742 CDQ分治,三维LIS
HDU4742 CDQ分治,三维LIS 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4742 题意: 每个球都有三个属性值x,y,z,要求最长的lis的 ...
- HDU-4742 Pinball Game 3D 三维LIS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4742 题意:求3维的LIS.. 用分治算法搞得,参考了cxlove的题解.. 首先按照x排序,然后每个 ...
- hdu4742 Pinball Game 3D
真他娘的搞不懂cdq分治的顺序问题.但是candy?的博客里提到过,多想想吧-- #include <algorithm> #include <iostream> #inclu ...
- UvaLive 6667 Longest Chain (分治求三元组LIS&树状数组)
题目链接: here 题意: 和hdu4742类似.差别就是一部分三元组是直接给出的.另一部分是用他给的那个函数生成的.还有就是这里的大于是严格的大于a>b必须ax>bx,ay>by ...
- bryce1010专题训练——CDQ分治
Bryce1010模板 CDQ分治 1.与普通分治的区别 普通分治中,每一个子问题只解决它本身(可以说是封闭的) 分治中,对于划分出来的两个子问题,前一个子问题用来解决后一个子问题而不是它本身 2.试 ...
- SPOJ Another Longest Increasing Subsequence Problem 三维最长链
SPOJ Another Longest Increasing Subsequence Problem 传送门:https://www.spoj.com/problems/LIS2/en/ 题意: 给 ...
- UVA live 6667 三维严格LIS
UVA live 6667 三维严格LIS 传送门:https://vjudge.net/problem/UVALive-6667 题意: 每个球都有三个属性值x,y,z,要求最长的严格lis的长度和 ...
随机推荐
- MacDev.GarbageCollectionIsDeprecated-WhenXcodeCompileMacAppProject
Garbage Collection is not supported 当Xcode编译Mac OSX App时报错:"Garbage Collection is not supported ...
- gdal gdal2tiles.py 的使用
I’m here showing how you can use GDAL2Tiles to generate map tiles of Tom Patterson’s Natural Earth I ...
- Luogu 1169 [ZJOI2007]棋盘制作 - 动态规划+单调栈
Description 给一个01矩阵, 求出最大的01交错的正方形和最大的01交错的矩阵 Solution 用动态规划求出最大的正方形, 用单调栈求出最大的矩阵. 在这里仅介绍求出最大正方形(求最大 ...
- wmi uuid
[转]https://www.cnblogs.com/-sylar/p/8376621.html 1. 开始-运行-输入:wbemtest 回车2. 单击"连接", 输入:root ...
- Spring ApplicationContext(一)初始化过程
Spring 容器 ApplicationContext(一)初始化过程 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) ...
- nginx记录post数据日志
1.vi nginx.conf 找到http {}中log_foramt ,定义post 日志格式 #log_format main '$remote_addr - $remote_user [$ti ...
- xcode资源管理
1. 在根目录放图片. UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ok.pn ...
- phantomjs问题收集
1. phantomjs如果遇到重定向的url,两个页面都会加载,onInitialized只会执行一次,onResourceReceived会执行两次.我试过在onInitialized加一段代码, ...
- unity luaFramework
1 AppConst: DebugMode: 调试模式,true:lua脚本直接读取自 AssetDir,false:开始会将AssetDir内的lua脚本复制到 Util.DataPath内(根据平 ...
- win7 64位远程连接oracle11g64位
1.首先下载即时客户端 instantclient-basic-windows.x64-11.2.0.4.0,下载地址:http://www.oracle.com/technetwork/topics ...