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.

InputThere 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 
OutputFor 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 题意
给你两个序列Li,Ri,求构造一个最长的子序列,使得L递减, R递增。在保证最长的前提下要求字典序最小。(vj)
思路:
很明显的可以看出是三维偏序问题。
但是这题要求的是最长的序列,而不是对于每一个元素求解,所以需要要对CDQ分治的部分做相应的更改。
我们设对每一个元素,求以它未开始的符合题意的序列长度最长是多长。只有这样,才能顺利求出字典序最小。
那么对于每一个元素,在处理其答案时,其右侧的元素答案都应该已知了。
传统分治的做法显然不能满足这个要求,因为在如果先治左边,那么右边未知,便无法知道答案。
于是我们想到先分治右侧,再分治左侧。
但是这还不够,在对左侧求解过程中,当前右侧的答案可能并非是全局的答案,这一点的原因我不知道该如何描述,但是在3层CDQ里面就已经很明显了。
所以我们先分治右边,在处理当前层,再分治左边。
由于分治顺序的特殊性,归并排序已经不适用了,所以只能使用快排。
注意在处理左侧之前,要回复左侧原来的顺序。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime> #define fuck(x) clog<<#x<<" = "<<x<<endl;
#define debug(a, x) clog<<#a<<"["<<x<<"] = "<<a[x]<<endl;
#define lson l,mid,ls
#define rson mid+1,r,rs
#define ls (rt<<1)
#define rs ((rt<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int loveisblue = ;
const int maxn = ;
const int maxm = ;
const int inf = 0x3f3f3f3f;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); int n;
struct node{
int a,b,c,ans;
}cdq[maxn];
int mx[maxn<<];
void update(int l,int r,int rt,int pos,int val){
if(l==r){
mx[rt]=val;
return;
}
int mid = (l+r)>>;
if(pos<=mid){
update(lson,pos,val);
}else{
update(rson,pos,val);
}
mx[rt]=max(mx[ls],mx[rs]);
} int query(int l,int r,int rt,int L,int R){
if(L<=l&&R>=r){
return mx[rt];
}
int ans = ;
int mid = (l+r)>>;
if(L<=mid){
ans= max(ans,query(lson,L,R));
}if(R>mid){
ans = max(ans,query(rson,L,R));
}
return ans;
}
int num[maxn];
int rem[maxn],tot;
int get_id(int x){
return lower_bound(rem+,rem++tot,x)-rem;
} bool cmp1(node a,node b){
if(a.b!=b.b)return a.b>b.b;
return a.c<b.c;
}
bool cmp(node a,node b){
return a.a<b.a;
} void solve(int l,int r){
if(l==r){ return;}
int mid = (l+r)>>;
solve(mid+,r);
int t1 = mid,t2 = r;
int cur = r+;
sort(cdq+l,cdq+mid+,cmp1);
sort(cdq+mid+,cdq+r+,cmp1);
while (t1>=l||t2>mid) {
if (t1 < l || (t2 > mid && cdq[t1].b >= cdq[t2].b)) {
update(, tot, , get_id(cdq[t2].c), cdq[t2].ans);
t2--;
} else {
cdq[t1].ans = max(cdq[t1].ans, query(, tot, , get_id(cdq[t1].c) ,tot)+);
t1--;
}
}
for(int i=mid+;i<=r;i++){
update(,tot,,get_id(cdq[i].c),);
}
sort(cdq+l,cdq+mid+,cmp);
solve(l,mid);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
while (scanf("%d",&n)!=EOF) {
tot = ;
for (int i = ; i <= n; i++) {
scanf("%d", &num[i]);
}
for (int i = ; i <= n; i++) {
int x;
scanf("%d", &x);
rem[++tot] = x;
cdq[i] = node{i, num[i], x, };
}
sort(rem+,rem++tot);
tot = unique(rem+,rem++tot)-rem-;
solve(,n);
sort(cdq+,cdq++n,cmp);
int ans = ;
for(int i=;i<=n;i++){
ans = max(cdq[i].ans,ans);
}
printf("%d\n",ans);
int last = ;
cdq[].b = inf;
cdq[].c = -;
for(int i=;i<=n;i++){
if(cdq[i].ans==ans&&cdq[i].b<=cdq[last].b&&cdq[i].c>=cdq[last].c){
ans--;
last=i;
if(ans==){printf("%d\n",i);}
else printf("%d ",i);
}
}
}
return ;
}

Boring Class HDU - 5324 (CDQ分治)的更多相关文章

  1. HDU 5324 Boring Class【cdq分治】

    这就是一个三维排序的问题,一维递减,两维递增,这样的问题用裸的CDQ分治恰好能够解决. 如同HDU 4742(三维排序,一个三维都是递增的) 由于最小字典序比較麻烦,所以要从后面往前面做分治.每一个点 ...

  2. HDU - 5324:Boring Class (CDQ分治&树状数组&最小字典序)

    题意:给定N个组合,每个组合有a和b,现在求最长序列,满足a不升,b不降. 思路:三位偏序,CDQ分治.   但是没想到怎么输出最小字典序,我好菜啊. 最小字典序: 我们倒序CDQ分治,ans[i]表 ...

  3. 2015 Multi-University Training Contest 3 hdu 5324 Boring Class

    Boring Class Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  4. HDU 3507 Print Article(CDQ分治+分治DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3507 [题目大意] 将长度为n的数列分段,最小化每段和的平方和. [题解] 根据题目很容易得到dp ...

  5. HDU 5730 Shell Necklace(CDQ分治+FFT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5730 [题目大意] 给出一个数组w,表示不同长度的字段的权值,比如w[3]=5表示如果字段长度为3 ...

  6. hdu 3842 Machine Works(cdq分治维护凸壳)

    题目链接:hdu 3842 Machine Works 详细题解: HDU 3842 Machine Works cdq分治 斜率优化 细节比较多,好好体会一下. 在维护斜率的时候要考虑x1与x2是否 ...

  7. hdu 5830 FFT + cdq分治

    Shell Necklace Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  8. cdq分治(hdu 5618 Jam's problem again[陌上花开]、CQOI 2011 动态逆序对、hdu 4742 Pinball Game、hdu 4456 Crowd、[HEOI2016/TJOI2016]序列、[NOI2007]货币兑换 )

    hdu 5618 Jam's problem again #include <bits/stdc++.h> #define MAXN 100010 using namespace std; ...

  9. 【 HDU - 4456 】Crowd (二维树状数组、cdq分治)

    BUPT2017 wintertraining(15) #5A HDU 4456 题意 给你一个n行n列的格子,一开始每个格子值都是0.有M个操作,p=1为第一种操作,给格子(x,y)增加z.p=2为 ...

随机推荐

  1. 神舟mini pcs-b wifi-bt 驱动

    最新mini pcs ssd硬盘版 安装win10后蓝牙设备找不到,显示usb获取设备符失败, 卸载wifi驱动,安装以下驱动,两个链接应该都可以. TW: https://downloadcente ...

  2. 模拟登录新浪微博(Python) - 转

    Update: 如果只是写个小爬虫,访问需要登录的页面,采用填入cookie 的方法吧,简单粗暴有效,详细见:http://www.douban.com/note/264976536/模拟登陆有时需要 ...

  3. Python3 写的远程批量修改文件内容的脚本

    一.说明: 1.利用Python的paramiko模块,调用远程的shell命令去修改相应文件. 2.有一个专用配置文件,列出服务器清单. 3.Python循环读取配置文件的服务器IP去连接它,并执行 ...

  4. spoj Distinct Substrings 后缀数组

    给定一个字符串,求不相同的子串的个数. 假如给字符串“ABA";排列的子串可能: A B A AB  BA ABA 共3*(3+1)/2=6种; 后缀数组表示时: A ABA BA 对于A和 ...

  5. 如何编写go代码

    go是一种静态编译型的语言,它的编译速度非常快. go的官方编译器称为gc,包括编译工具5g,6g和8g,连接工具5l,6l和8l.其中的数字表示处理器的架构.我们不必关心如何挑选这些工具,因为go提 ...

  6. 如何将英文PDF文献翻译成中文

    方法一:利用Google的本地文档翻译功能 这种方法比较简单,打开Google翻译首页http://translate.google.cn/, 选择"上传文档" 选择文件,开始翻译 ...

  7. 捕捉WPF应用程序中XAML代码解析异常

    原文:捕捉WPF应用程序中XAML代码解析异常 由于WPF应用程序中XAML代码在很多时候是运行时加载处理的.比如DynamicResource,但是在编译或者运行的过程中,编写的XAML代码很可能有 ...

  8. Hbase API: 写入Bigtable.

  9. @codeforces - 418D@ Big Problems for Organizers

    目录 @description@ @solution@ @accepted code@ @details@ @description@ n 个点连成一棵树,经过每条边需要花费 1 个单位时间. 现给出 ...

  10. [luogu P2617] Dynamic Rankings 带修主席树

    带修改的主席树,其实这种,已经不能算作主席树了,因为这个没有维护可持久化的... 主席树直接带修改的话,由于这种数据结构是可持久化的,那么要相应改动,这个节点以后所有的主席树,这样单次修改,就达到n* ...