zoukankan      html  css  js  c++  java
  • Poj 2533 Longest Ordered Subsequence(LIS)

    一、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代码
    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[n][n];    
        }    
        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));
            }
        }   
    } 


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    nginx+vue+thinkphp5.1部署,解决前端刷新404,以及前端404解决后,后台又404的问题
    centos7.4挂载硬盘
    thinkphp5.1+layui2.x 时间戳转换为日期格式
    解决linux(ubuntu18)下无法挂载ntfs磁盘,并读写挂载硬盘
    sublime中nodejs配置
    jquery 中的$("obj").html('')中的html动态改变之后点击事件失效
    js怎么弹出变量的数据类型
    异步操作执行后子页面重新修改父页面iframe高度
    iframe标签父页面高度自适应
    string.Format()方法、Graphics类、DrawImage方法
  • 原文地址:https://www.cnblogs.com/AndyDai/p/4734142.html
Copyright © 2011-2022 走看看