HDU-5324 cdq分治
来源:互联网 发布:如何获取淘宝pid 编辑:程序博客网 时间:2024/05/27 06:16
Boring Class
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1444 Accepted Submission(s): 460
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 sequencesL1,L2,...,Ln and R1,R2,...,Rn .
Your task is to find a longest subsequencev1,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.
Here is the problem:
Give you two sequences
Your task is to find a longest subsequence
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
Both of the following two lines contain n integers describe the two sequences.
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.
Output m integers in the next line.
Sample Input
55 4 3 2 16 7 8 9 1021 23 4
Sample Output
51 2 3 4 511
Author
ZSTU
Source
2015 Multi-University Training Contest 3
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5324
题目大意:
给出两个长度相同数列,求第一个不上升,第二个不下降的最长子序列长度。
解题思路:
二维的偏序关系问题,典型的cdq分治,稍微注意输出字典序最小的序列即可。
AC代码:
import java.util.*;public class Main {static int n,ct,ans,pos,x;static int[] aa=new int[50005];static int[] bb=new int[50005];static int[] cc=new int[50005];static int[] bit=new int[50005];static Pair[] pp=new Pair[50005];static Map<Integer,Integer> mp=new HashMap<Integer,Integer>();static void updata(int i,int x){for(;i<=ct;i+=i&-i)bit[i]=Math.max(bit[i],x);}static int max(int i){int res=0;for(;i>0;i-=i&-i)res=Math.max(res,bit[i]);return res;}static void clear(int i){for(;i<=ct;i+=i&-i)bit[i]=0;}static class Pair implements Comparable<Pair>{int x,y,z;Pair(int a,int b,int c){x=a;y=b;z=c;}public int compareTo(Pair p) {if(y==p.y) return z-p.z;return y-p.y;}}static void cdq(int l,int r){if(l==r) { cc[l]++;return;}int mid=(l+r)/2;cdq(mid+1,r);for(int i=l;i<=r;i++)pp[i]=new Pair(aa[i],bb[i],i);Arrays.sort(pp,l,r+1);;for(int i=r;i>=l;i--){x=pp[i].x;pos=pp[i].z;if(pos<=mid)cc[pos]=Math.max(cc[pos],max(x));else updata(x,cc[pos]);}for(int i=l;i<=r;i++)clear(pp[i].x);cdq(l,mid);}public static void main(String[] args) {Scanner in=new Scanner(System.in);while(in.hasNext()){n=in.nextInt();for(int i=1;i<=n;i++)aa[i]=cc[i]=in.nextInt();for(int i=1;i<=n;i++)bb[i]=in.nextInt();ct=0;Arrays.sort(cc,1,n+1);mp.clear();for(int i=1;i<=n;i++)if(cc[i]!=cc[i-1]) mp.put(cc[i],++ct);for(int i=1;i<=n;i++)aa[i]=mp.get(aa[i]);Arrays.fill(cc,0);Arrays.fill(bit,0);cdq(1,n);ans=pos=0;for(int i=1;i<=n;i++)if(cc[i]>ans) { ans=cc[i];pos=i;}System.out.println(ans);System.out.print(pos);for(int i=pos+1;i<=n;i++){if(cc[i]==ans-1&&aa[i]<=aa[pos]&&bb[i]>=bb[pos]){ans--;pos=i;System.out.print(" "+pos);}}System.out.println();}}}
0 1
- HDU-5324 cdq分治
- hdu 5324 树套树、cdq分治
- HDU 5324 Boring Class【cdq分治】
- HDU 5324 Boring Class(CDQ分治)
- Hdu 5324 Boring Class (cdq分治)
- HDU 5324 (CDQ分治 树状数组)
- HDU 4456 CDQ分治
- HDU - 1166 CDQ分治
- Hdu 5618 CDQ分治
- hdu 5126 stars cdq分治
- HDU 4456 Crowd (cdq分治)
- HDU 5730 (CDQ分治 FFT)
- HDU 5552 (CDQ分治 NTT)
- HDU 5896 (CDQ分治 NTT)
- HDU 5552 CDQ分治+NTT
- HDU 5896 CDQ分治+NTT
- Hdu-5126 stars(cdq分治)
- HDU 5324 Boring Class 树套树 或 CDQ分治
- java停止线程
- 睡眠和休眠是不一样的
- Codeforces 615C. Running Track
- MySQL在MacOS下修改datadir目录终于成功了
- 安卓小日记练习(3)listview的多个item
- HDU-5324 cdq分治
- Android开源组件-动态效果定值范围选择控件
- [Machine Learning]随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )的对比
- 51Nod-1391-01串
- vi/vim 使用方法讲解
- 安卓开发学习笔记
- Vert.x 核心模块 DNS(十五)
- 特别帅的“Vim的分屏功能”
- C++培训_001_WIN10的安装与激活_VS编译器的安装