HDU-5324 cdq分治

本文介绍了一种寻找两组数列中第一个不上升、第二个不下降的最长子序列的问题,并提供了一个具体的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();
		}
	}
}


### 关于HDU - 6609 的题目解析 由于当前未提供具体关于 HDU - 6609 题目的详细描述,以下是基于一般算法竞赛题型可能涉及的内容进行推测和解答。 #### 可能的题目背景 假设该题目属于动态规划类问题(类似于多重背包问题),其核心在于优化资源分配或路径选择。此类问题通常会给出一组物品及其属性(如重量、价值等)以及约束条件(如容量限制)。目标是最优地选取某些物品使得满足特定的目标函数[^2]。 #### 动态转移方程设计 如果此题确实是一个变种的背包问题,则可以采用如下状态定义方法: 设 `dp[i][j]` 表示前 i 种物品,在某种条件下达到 j 值时的最大收益或者最小代价。对于每一种新加入考虑范围内的物体 k ,更新规则可能是这样的形式: ```python for i in range(n): for s in range(V, w[k]-1, -1): dp[s] = max(dp[s], dp[s-w[k]] + v[k]) ``` 这里需要注意边界情况处理以及初始化设置合理值来保证计算准确性。 另外还有一种可能性就是它涉及到组合数学方面知识或者是图论最短路等相关知识点。如果是后者的话那么就需要构建相应的邻接表表示图形结构并通过Dijkstra/Bellman-Ford/Floyd-Warshall等经典算法求解两点间距离等问题了[^4]。 最后按照输出格式要求打印结果字符串"Case #X: Y"[^3]。 #### 示例代码片段 下面展示了一个简单的伪代码框架用于解决上述提到类型的DP问题: ```python def solve(): t=int(input()) res=[] cas=1 while(t>0): n,k=list(map(int,input().split())) # Initialize your data structures here ans=find_min_unhappiness() # Implement function find_min_unhappiness() res.append(f'Case #{cas}: {round(ans)}') cas+=1 t-=1 print("\n".join(res)) solve() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值