[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, ...
随机推荐
- hdu3078 伪LCA……
题意:有 n 点的一颗树,每个节点有格子的权值,现在有两种操作,修改一个点的权值,或者求两点之间的路径上的第 k 大的权值. 其实看到这个题,就在 YY 各种做法,询问后得到貌似可能是关于主席树.树链 ...
- Python中optionParser模块的使用方法[转]
本文以实例形式较为详尽的讲述了Python中optionParser模块的使用方法,对于深入学习Python有很好的借鉴价值.分享给大家供大家参考之用.具体分析如下: 一般来说,Python中有两个内 ...
- pt-find 使用实例
pt-find - Find MySQL tables and execute actions, like GNU find. 用法:pt-find [OPTION...] [DATABASE...] ...
- 用DotNetBar设计的 Gradient Buttons 漂亮按钮
http://www.webdesignerwall.com/demo/css-buttons.html public class GradientButtons : DevComponents ...
- [solr] - 环境搭建2
前面使用Tomcat搭建solr,参考文章: http://www.cnblogs.com/HD/p/3977799.html 原来solr可以不使用tomcat/jboss等服务器,它自身已经集成了 ...
- 【freemaker】之文本,html文本,去除空格,字母大小写,循环数组,字符串截取,map取值,遍历map
测试代码 @Test public void test06(){ try { root.put("emp", "<span color='red'>你好张三& ...
- JSP+servlet简单登录实例
一个简单的jsp+servlet实例,实现简单的登录 转载▼ http://blog.sina.com.cn/s/blog_5c5bc9070100z7wb.html 开发环境myeclips ...
- mysql:字符串转换为日期类型
函数:DATE_FORMAT http://www.w3school.com.cn/sql/func_date_format.asp
- (C#) 求两个数组的交集
基本上在面试的时候,会具体到两个int数组,或string数组.具体也就是讨论算法. 首先需要的是和面试的人确认题目的含义,并非直接答题. 然后,可以提出自己的想法,首先最快的是用linq { Lis ...
- ubuntu14.04配置impala的odbc连接
cdh hive和impala的odbc驱动 http://www.cloudera.com/downloads.html.html 选择 SLES 11 64-bit下载: http://www.c ...