Poj 2533 Longest Ordered Subsequence(LIS)
2013-08-02 10:02
447 查看
一、Description
A numeric sequence of ai is ordered if
a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1,
a2, ..., aN) be any sequence (ai1,
ai2, ..., aiK), where 1 <=
i1 < i2 < ... < iK <=
N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Input
The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000
Output
Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.
二、题解
这题和3903、1887一样属于LIS问题,在之前有关于LIS的分析 最长递增子序列(LIS)。这里实现了LCS+快速排序的方法。比较耗时、耗内存,但对于单个Case还是可以保证运行的。
三、java代码
A numeric sequence of ai is ordered if
a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1,
a2, ..., aN) be any sequence (ai1,
ai2, ..., aiK), where 1 <=
i1 < i2 < ... < iK <=
N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Input
The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000
Output
Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.
二、题解
这题和3903、1887一样属于LIS问题,在之前有关于LIS的分析 最长递增子序列(LIS)。这里实现了LCS+快速排序的方法。比较耗时、耗内存,但对于单个Case还是可以保证运行的。
三、java代码
import java.util.*; public class Main { static int n; static int[] a; static int[] b; public static void QuickSort(int[] a){ QSort(a,1,n); } public static void QSort(int[] a,int p,int r){ if(p<r) { int q=Partition(a,p,r); QSort(a,p,q-1); QSort(a,q+1,r); } } public static int Partition(int[] a,int p,int r){ int x=a[r]; int i=p-1; for(int j=p;j<r;j++) { if(a[j]<=x){ i=i+1; swap(a, i, j); } } swap(a, i+1, r); return i+1; } public static void swap(int[] a, int i,int j){ int temp; temp=a[j]; a[j]=a[i]; a[i]=temp; } public static int LCS(int a[],int[] b){ int [][] z=new int [n+1][n+1]; int i,j; for( i=0;i<=n;i++) z[i][0]=0; for( j=0;j<=n;j++) z[0][j]=0; for(i=1;i<=n;i++){ for( j=1;j<=n;j++){ if(a[i]==b[j]){ z[i][j]= z[i-1][j-1]+1; } else z[i][j]=z[i-1][j] > z[i][j-1] ?z[i-1][j]:z[i][j-1]; } } return z ; } public static void main(String[] args) { Scanner cin = new Scanner(System.in); while(cin.hasNext()){ n=cin.nextInt(); a=new int[n+1]; b=new int[n+1]; int i,j; for(i=1;i<=n;i++){ a[i]=cin.nextInt(); b[i]=a[i]; } QuickSort(a); for(i=1;i<n;i++){ for(j=i+1;j<=n;j++){ if(a[i]!=-1 && a[i]==a[j]) a[j]=-1; } } System.out.println(LCS(a,b)); } } }
相关文章推荐
- POJ 2533 Longest Ordered Subsequence (LIS)
- POJ 2533 Longest Ordered Subsequence(dp LIS)
- POJ - 2533 -Longest Ordered Subsequence(LIS模板题)
- POJ 2533-Longest Ordered Subsequence(裸LIS)
- POJ-2533-Longest Ordered Subsequence(LIS模板)
- POJ 2533 Longest Ordered Subsequence(LIS:最长上升子序列)
- 【DP|LIS】POJ-2533 Longest Ordered Subsequence(O(n^2)、O(nlogn))
- POJ2533-Longest Ordered Subsequence(LIS)
- poj 2533 Longest Ordered Subsequence(LIS(最长上升子序列))
- LIS(n^2) POJ 2533 Longest Ordered Subsequence
- POJ 2533 Longest Ordered Subsequence(LIS)
- POJ 2533 Longest Ordered Subsequence(最长非递减子序列,LIS)
- POJ:2533 Longest Ordered Subsequence(LIS-n*logn解法)
- 【dp-LIS】牛客网 --最长上升子序列 POJ 2533--Longest Ordered Subsequence(LIS模板题)
- POJ-2533(LIS) Longest Ordered Subsequence
- POJ 2533 : Longest Ordered Subsequence - LIS问题
- POJ 2533 Longest Ordered Subsequence DP(LIS)
- poj - 2533 - Longest Ordered Subsequence(LIS)
- POJ2533:Longest Ordered Subsequence(LIS)
- POJ 2533 Longest Ordered Subsequence(LIS)