题目链接: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的更多相关文章

  1. [2015hdu多校联赛补题]hdu5384 Danganronpa

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5384 题意:函数f(A, B)定义:A.B为字符串,f(A, B)为A中有多少个不同的B(ex:f(& ...

  2. [2015hdu多校联赛补题]hdu5302 Connect the Graph

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5302 题意:给你一个无向图,它的边要么是黑色要么是白色,且图上的每个点最多与两个黑边两个白边相连.现在 ...

  3. [2015hdu多校联赛补题]hdu5301 Buildings

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5301 题目大意:给你一块由1x1方格组成的矩形区域,其中有且仅有一个坏块,现在你要在上面建矩形的房子, ...

  4. [2015hdu多校联赛补题]hdu5378 Leader in Tree Land

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5378 题意:给你一棵n个结点的有根树.因为是有根树,那么每个结点可以指定以它为根的子树(后面讨论的子树 ...

  5. [2015hdu多校联赛补题]hdu5372 Segment Game

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5372 题意:进行n次操作,操作分两种,0和1,每一个0操作按出现顺序有一个编号(从1开始 0操作 0 ...

  6. [2015hdu多校联赛补题]hdu5371 Hotaru's problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-se ...

  7. [2015hdu多校联赛补题]hdu5303 Delicious Apples

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...

  8. [2015hdu多校联赛补题]hdu5299 Circles Game

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5299 题意: 在欧几里得平面上有n个圆,圆之间不会相交也不会相切,现在Alice和Bob玩游戏,两人轮 ...

  9. [2015hdu多校联赛补题]hdu5348 MZL's endless loop

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1, ...

随机推荐

  1. linux crontab -r 导致no crontab for root的原因及解决方案

    使用方式 : crontab file [-u user]-用指定的文件替代目前的crontab. crontab-[-u user]-用标准输入替代目前的crontab. crontab-1[use ...

  2. phpwind之关闭账号通

    phpwind的账号通功能早就失效了,但是首页的链接一直存在,造成了很不好的影响 但是后台打开账号通功能又打不开,所以想到了在前端的模板中通过屏蔽这部分代码的方法隐藏掉这个功能在首页的显示 1.打开/ ...

  3. java.lang.Enum<E extends Enum<E>>

    public enum Direction { L, LU, U, RU, R, RD, D, LD, STOP, JUMP;} for(Direction d: Direction.values() ...

  4. Android项目——读取手机联系人信息

    加入读取联系人信息的权限 <uses-permission android:name="android.permission.READ_CONTACTS"/> cont ...

  5. js中的this指针(二)

    在 js 中声明并定义一个函数后,除了定义时传入的形式参数,函数还会接收到 2 个附加的参数:this 和 arguments. this 指针的值取决于调用时的模式. 当这个函数被保存为对象的一个属 ...

  6. 032. asp.netWeb用户控件之一初识用户控件并为其自定义属性

    Web用户控件的优点: 可以将常用的内容或者控件以及控件的运行程序逻辑, 设计为用户控件, 以后便可以在多个页面中重复使用该用户控件, 从而省去许多重复性的工作. 如网页上的导航栏, 几乎每个页面都需 ...

  7. Oracle导入和导出

    导出:EXP userid=<username>/<password>@<service_name> file=<dmpname> e.g.exp sa ...

  8. Spring技术揭幕----DispatcherServlet

    Spring MVC是一个MVC模式的实现.在Spring MVC的使用中,需要在web.xml中配置DispatcherServlet,也就是说其核心是一个Servlet,这个DispatcherS ...

  9. zero ice 如何获取本地以及对端IP 地址及端口号

    // 在客户端显示端点连接IP及端口号信息 static void show_endpoint(const Ice::ObjectPrx &proxy) { Ice::ConnectionPt ...

  10. 14.高度最小的BST

    题目描述 对于一个元素各不相同且按升序排列的有序序列,请编写一个算法,创建一棵高度最小的二叉查找树. 给定一个有序序列int[] vals,请返回创建的二叉查找树的高度. 返回高度的代码如下: imp ...