HDU-5324 cdq分治

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 sequences  L1,L2,...,Ln  and  R1,R2,...,Rn .
Your task is to find a longest subsequence  v1,v2,...vm  satisfies
v11 , vmn , vi<vi+1  .(for i from 1 to m - 1)
LviLvi+1 , RviRvi+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.
1n50000
Both of the following two lines contain n integers describe the two sequences.
1Li,Ri109
 

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分治,稍微注意输出字典序最小的序列即可。

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值