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 ;
}

最新文章

  1. js
  2. NYOJ 536 开心的mdd(DP)
  3. Linux学习心得之 双显卡、中文输入法及svn初步使用
  4. Java band [Cite]
  5. JavaScript高级 函数表达式 《JavaScript高级程序设计(第三版)》
  6. 解决 emulator-5554 disconnected !Cancelling错误
  7. Tempo 2.0
  8. 使用Vitamio打造自己的Android万能播放器(7)——在线播放(下载视频)
  9. Python Django连接(听明白了是连接不是创建!)Mysql已存在的数据库
  10. 前端——Bootstrap
  11. [JavaScript]catch(ex)语句中的ex
  12. Install Kernel 3.10 on CentOS 6.5
  13. ormlite 文档
  14. Git下基本命令操作
  15. curl Array to string conversion 错误
  16. jQuery/CSS3类似阿里巴巴的商品导航菜单实现教程
  17. python3制作捧腹网段子页爬虫
  18. 记录下MD5加密遇到的坑
  19. Locust性能测试3 no-web运行
  20. Problem I: 零起点学算法30——输出四位完全平方数

热门文章

  1. Cocos2d-x 3.0 红孩儿私家必修 - 第一章 初识Cocos2d-x 3.0project
  2. uiautomator中InteractionController学习笔记(8)
  3. code::blocks配置编译cuda并进行第一个demo的測试
  4. elcipse 编译cocos2d-x android
  5. Swift - 使用CollectionView实现图片Gallery画廊效果(左右滑动浏览图片)
  6. 杂项-分布式:Hadoop
  7. 院校-德国:亚琛工业大学(RWTH)
  8. Asp.Net Core部署到Linux服务器
  9. ThinkPHP5(目录,路径,模式设置,命名空间)
  10. 杭电 4508 湫湫系列故事——减肥记I【完全背包】