Boring Class

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 900    Accepted Submission(s): 247

Problem Description

Mr. Zstu and Mr. Hdu are taking a boring class , Mr. Zstu comes up with a problem to kill time, Mr. Hdu thinks it’s too easy, he solved it very quickly, what about you guys?
Here is the problem:
Give you two sequences L1,L2,...,Ln and R1,R2,...,Rn.
Your task is to find a longest subsequence v1,v2,...vm satisfies
v1≥1,vm≤n,vi<vi+1 .(for i from 1 to m - 1)
Lvi≥Lvi+1,Rvi≤Rvi+1(for i from 1 to m - 1)
If there are many longest subsequence satisfy the condition, output the sequence which has the smallest lexicographic order.

Input
There are several test cases, each test case begins with an integer n.
1≤n≤50000
Both of the following two lines contain n integers describe the two sequences.
1≤Li,Ri≤109

Output
For each test case ,output the an integer m indicates the length of the longest subsequence as described.
Output m integers in the next line.
 
Sample Input
5
5 4 3 2 1
6 7 8 9 10
2
1 2
3 4
 
Sample Output
5
1 2 3 4 5
1
1
 
Author
ZSTU
 
Source

解题:CDQ分治

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct Node {
int l,r,id;
bool operator<(const Node &t) const {
if(r != t.r) return r < t.r;
if(l != t.l) return l > t.l;
return id < t.id;
}
} P[maxn],A[maxn],B[maxn];
int n,tot,Li[maxn],C[maxn],dp[maxn];
void update(int i,int val) {
for(; i <= tot; i += i&(-i))
C[i] = max(C[i],val);
}
void clr(int i) {
for(; i <= tot; i += i&(-i)) C[i] = ;
}
int query(int i) {
int ret = ;
for(; i > ; i -= i&(-i)) ret = max(ret,C[i]);
return ret;
}
void cdq(int L,int R) {
if(L == R) {
dp[P[L].id] = max(dp[P[L].id],);
return;
}
int mid = (L + R)>>;
cdq(mid+,R);
int a = ,b = ;
for(int i = L; i <= mid; ++i) A[a++] = P[i];
for(int i = mid+; i <= R; ++i) B[b++] = P[i];
sort(A,A+a);
sort(B,B+b);
int j = b-;
for(int i = a-; i >= ; --i) {
for(; j >= && B[j].r >= A[i].r; --j)
update(B[j].l,dp[B[j].id]);
dp[A[i].id] = max(dp[A[i].id],query(A[i].l) + );
}
for(int i = ; i < b; ++i) clr(B[i].l);
cdq(L,mid);
}
int main() {
while(~scanf("%d",&n)) {
memset(dp,,sizeof dp);
memset(C,,sizeof C);
for(int i = tot = ; i < n; ++i) {
scanf("%d",&P[i].l);
P[i].id = i;
Li[tot++] = P[i].l;
}
for(int i = ; i < n; ++i) {
scanf("%d",&P[i].r);
Li[tot++] = P[i].r;
}
sort(Li,Li + tot);
tot = unique(Li, Li + tot) - Li;
for(int i = ; i < n; ++i) {
P[i].l = lower_bound(Li,Li+tot,P[i].l) - Li + ;
P[i].r = lower_bound(Li,Li+tot,P[i].r) - Li + ;
}
cdq(,n-);
int ret = ,pre = -;
for(int i = ; i < n; ++i) ret = max(ret,dp[i]);
printf("%d\n",ret);
for(int i = ; i < n; ++i) {
if(dp[i] == ret && (pre == - || P[i].l <= P[pre].l && P[i].r >= P[pre].r)) {
if(pre != -) putchar(' ');
printf("%d", + i);
--ret;
pre = i;
}
}
puts("");
}
return ;
}

2015 Multi-University Training Contest 3 hdu 5324 Boring Class的更多相关文章

  1. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  2. 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!

    Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...

  3. 2015 Multi-University Training Contest 8 hdu 5385 The path

    The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...

  4. 2015 Multi-University Training Contest 3 hdu 5317 RGCDQ

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  5. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  6. 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  7. 2015 Multi-University Training Contest 6 hdu 5362 Just A String

    Just A String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  8. 2015 Multi-University Training Contest 6 hdu 5357 Easy Sequence

    Easy Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. 2015 Multi-University Training Contest 7 hdu 5378 Leader in Tree Land

    Leader in Tree Land Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

随机推荐

  1. POJ 3301

    开始就是瞄着三分来做的,但看题目,感觉是旋转卡壳吧..可是,用了旋转卡壳还三分条毛啊.. 可以令正方形不旋转,而改为令点绕原点旋转,这样,很好的解决了问题,就可以比较X轴最大长度和Y轴最大长度来确定正 ...

  2. 关于C++构造函数一二

    关于构造函数的调用顺序: 1.继承关系 2.从属关系 3.static声明的从属关系 关于拷贝构造函数的声明: classname(const classname & rhs) #includ ...

  3. SwipeRefreshLayout与ViewPager滑动事件冲突解决

    问题描写叙述: 开发中发现,SwipeRefreshLayout的下拉刷新,与ViewPager开发的banner的左右滑动事件有一点冲突,导致banner的左右滑动不够顺畅. 非常easy在bann ...

  4. POJ-3984-迷宫问题-BFS(广搜)-手写队列

    题目链接:id=3984">http://poj.org/problem? id=3984 这个本来是个模板题,可是老师要去不能用STL里的queue,得自己手写解决.ORZ....看 ...

  5. 通过PowerShell卸载全部的SharePoint 2010 解决方式

    通过PowerShell卸载全部的SharePoint 2010 解决方式         为了演示.我常常须要拆毁再重建SharePoint 2010 环境.         我常常须要用到的操作就 ...

  6. js 回调函数小例子

    js 回调函数小例子 <script> //将函数作为另一个函数的参数 function test1(){ alert("我是test1"); } function t ...

  7. 最小生成树基础 (Kruskal)

    最小生成树 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  8. js 预加载图片image()函数

    创建一个Image对象:var a=new Image();    定义Image对象的src: a.src=”xxx.gif”;    这样做就相当于给浏览器缓存了一张图片. 图像对象: 建立图像对 ...

  9. Vue读书笔记:关于$ref、props和$emit

    1.props实现父组件向子组件传递数据 子组件可以通过props接收到来自父组件的数据,并且是单向绑定的.也就是说,数据不能从子组件反向传递. 2.$ref实现子组件向父组件通信 来自官方非常难理解 ...

  10. Spring学习笔记(一) 简介

    版权声明 本文是摘自IBM上Naveen Balani的一篇文章,原文请点击此处:http://www.ibm.com/developerworks/cn/java/wa-spring1/ Sprin ...