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