[2015hdu多校联赛补题]hdu5324 Boring Class
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5324
题意:给你一个二维的序列,让你找出最长的第一维升第二维降的子序列(如果多个答案,输出字典序最小)
解:考虑从后往前dp,在u点你需要知道u点之后的比u的第一维小,第二维大的dp最大值
可以用分治枚举u点之后比u的第一维大的点,然后用树状数组查询比u的第二维小的点中dp最大的
具体是:
dp[i]表示以 i 开头的最长子序列,从后往前更新。
更新u点时有u.dp=max(v.dp)+1;v满足v.x<=u.x,v.y>=u.y,且v的在序列中的u的后面
我们用分治枚举u后面y值比u.y大的点v,然后把以v.x为序号v.dp为值插入树状数组中,就可以O(logN)查询到v.x<=u.x的dp最大值了
总时间复杂度:O(N*logN*logN)
/*
* Problem: hdu5324 Boring Class
* Author: SHJWUDP
* Created Time: 2015/8/3 星期一 21:14:55
* File Name: 233.cpp
* State: Accepted
* Memo: 多维变量的后缀区间查询
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int INF=0x7f7f7f7f; const int MaxA=5e4+; struct Node {
int x, y, id;
bool operator<(const Node & rhs) const {
return y<rhs.y;
}
};
struct Hash : vector<int> { //离散化
void prepare() {
sort(begin(), end());
erase(unique(begin(), end()), end());
}
int get(int x) {
return lower_bound(begin(), end(), x)-begin()+;
}
};
struct Fenwick {
int n;
vector<pair<int, int> > c;
void init(int n) {
this->n=n;
c.assign(n+, make_pair(, -INF));
}
int lowbit(int x) {
return x & -x;
}
void modify(int x, pair<int, int> v) {
while(x<=n) {
c[x]=v; x+=lowbit(x);
}
}
void update(int x, pair<int, int> v) {
while(x<=n) {
c[x]=max(c[x], v); x+=lowbit(x);
}
}
pair<int, int> query(int x) {
pair<int, int> res(, -INF);
while(x>) {
res=max(res, c[x]); x-=lowbit(x);
}
return res;
}
} fw; int n;
vector<Node> arr, tmp;
vector<pair<int, int> > dp; //dp[i].pair(以i开头的最长子序列长度, -子序列中下一个位置)
void dc(int l, int r) {
if(l==r) return;
int m=(l+r)>>;
dc(m+, r);
for(int i=l; i<=r; i++) tmp[i]=arr[i];
sort(tmp.begin()+l, tmp.begin()+m+);
sort(tmp.begin()+m+, tmp.begin()+r+);
int pr=r;
for(int i=m; i>=l; i--) {
int cid=tmp[i].id;
while(pr>m && tmp[pr].y>=tmp[i].y) {
fw.update(tmp[pr].x, make_pair(dp[tmp[pr].id].first+, -tmp[pr].id));//将第二维大于当前点的都加到树状数组里面
pr--;
}
dp[cid]=max(dp[cid], fw.query(tmp[i].x));
}
for(int i=m+; i<=r; i++) fw.modify(tmp[i].x, make_pair(, -INF));//用完树状数组后清空
dc(l, m);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif
while(~scanf("%d", &n)) {
arr.resize(n+); tmp.resize(n+);
Hash hash;
for(int i=; i<=n; i++) {
scanf("%d", &arr[i].x);
arr[i].id=i;
hash.push_back(arr[i].x);
}
hash.prepare();
for(int i=; i<=n; i++) arr[i].x=hash.get(arr[i].x);
hash.clear();
for(int i=; i<=n; i++) {
scanf("%d", &arr[i].y);
hash.push_back(arr[i].y);
}
hash.prepare();
for(int i=; i<=n; i++) arr[i].y=hash.get(arr[i].y); fw.init(n+);
dp.assign(n+, make_pair(, -INF));
dc(, n);
pair<int, int> ans(, -INF);
int stPos;
for(int i=; i<=n; i++) {
if(dp[i]>ans) {
ans=dp[i]; stPos=i;
}
}
printf("%d\n", ans.first);
printf("%d", stPos);
for(int i=-ans.second; i!=INF; i=-dp[i].second) printf(" %d", i);
printf("\n");
}
return ;
}
hdu5324
[2015hdu多校联赛补题]hdu5324 Boring Class的更多相关文章
- [2015hdu多校联赛补题]hdu5384 Danganronpa
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:函数f(A, B)定义:A.B为字符串,f(A, B)为A中有多少个不同的B(ex:f(& ...
- [2015hdu多校联赛补题]hdu5302 Connect the Graph
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5302 题意:给你一个无向图,它的边要么是黑色要么是白色,且图上的每个点最多与两个黑边两个白边相连.现在 ...
- [2015hdu多校联赛补题]hdu5301 Buildings
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5301 题目大意:给你一块由1x1方格组成的矩形区域,其中有且仅有一个坏块,现在你要在上面建矩形的房子, ...
- [2015hdu多校联赛补题]hdu5378 Leader in Tree Land
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:给你一棵n个结点的有根树.因为是有根树,那么每个结点可以指定以它为根的子树(后面讨论的子树 ...
- [2015hdu多校联赛补题]hdu5372 Segment Game
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5372 题意:进行n次操作,操作分两种,0和1,每一个0操作按出现顺序有一个编号(从1开始 0操作 0 ...
- [2015hdu多校联赛补题]hdu5371 Hotaru's problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-se ...
- [2015hdu多校联赛补题]hdu5303 Delicious Apples
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...
- [2015hdu多校联赛补题]hdu5299 Circles Game
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5299 题意: 在欧几里得平面上有n个圆,圆之间不会相交也不会相切,现在Alice和Bob玩游戏,两人轮 ...
- [2015hdu多校联赛补题]hdu5348 MZL's endless loop
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1, ...
随机推荐
- JSBinding+SharpKit / 菜单介绍
- Android学习六:Socket 使用
1socket的作用 通过http去获取服务器的数据在有些情况下是行不通的,所有使用socket与服务器通信也是必须掌握的 2.代码 好了上代码,代码中有解释,首先是简单的服务端代码 package ...
- [Spring MVC] - Interceptor 拦截器
Spring MVC中的Interceptor与Struts2的差不多. 下面是一个简单的Interceptor登陆验证例子: 1.需要在spring的配置文件中加入这段: <!-- 自定义拦截 ...
- Apache Shiro 使用手册(二)Shiro 认证
认证就是验证用户身份的过程.在认证过程中,用户需要提交实体信息(Principals)和凭据信息(Credentials)以检验用户是否合法.最常见的"实体/凭证"组合便是&quo ...
- BP(back propagation)反向传播
转自:http://www.zhihu.com/question/27239198/answer/89853077 机器学习可以看做是数理统计的一个应用,在数理统计中一个常见的任务就是拟合,也就是给定 ...
- 修复eclipse中使用mave update project后JRE都变成1.5的问题
在项目中的parent pom中添加如下代码即可 <properties> <project.build.sourceEncoding>UTF-8</project.bu ...
- CRM 权限与分派不一样问题
问题描述: 1 userA用户 为 区域经理; 2 区域经理(角色) 为 分派给userA的安全角色; 3 区域经理(角色) 设置了对实体 客户 的读权限为 业务部门级; 按正常来说userA 应 ...
- 剑指offer习题集1
1.打印二叉树 程序很简单,但是其中犯了一个小错误,死活找不到,写代码要注意啊 这里左右子树,要注意是node->left,结果写成root->left vector<int> ...
- Chap5: question: 29 - 31
29. 数组中出现次数超过一半的数字. 方法a. 排序取中 O(nlogn). 方法b. partition 函数分割找中位数 >=O(n). 方法c. 设计数变量,扫描一遍 ...
- FIO使用指南
前言 fio是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, nul ...