一、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));
}
}
}

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

Poj 2533 Longest Ordered Subsequence(LIS)的更多相关文章

  1. POJ 2533 Longest Ordered Subsequence(LIS模版题)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 47465   Acc ...

  2. POJ 2533 Longest Ordered Subsequence (LIS DP)

    最长公共自序列LIS 三种模板,但是邝斌写的好像这题过不了 N*N #include <iostream> #include <cstdio> #include <cst ...

  3. POJ 2533——Longest Ordered Subsequence(DP)

    链接:http://poj.org/problem?id=2533 题解 #include<iostream> using namespace std; ]; //存放数列 ]; //b[ ...

  4. POJ 2533 Longest Ordered Subsequence(裸LIS)

    传送门: http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 6 ...

  5. 题解报告:poj 2533 Longest Ordered Subsequence(最长上升子序列LIS)

    Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence ...

  6. POJ 2533 Longest Ordered Subsequence(dp LIS)

    Language: Default Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  7. POJ 2533 Longest Ordered Subsequence(最长上升子序列(NlogN)

    传送门 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subseque ...

  8. POJ 2533 Longest Ordered Subsequence(DP 最长上升子序列)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38980   Acc ...

  9. poj 2533 Longest Ordered Subsequence(线性dp)

    题目链接:http://poj.org/problem?id=2533 思路分析:该问题为经典的最长递增子序列问题,使用动态规划就可以解决: 1)状态定义:假设序列为A[0, 1, .., n],则定 ...

随机推荐

  1. elasticsearch从入门到出门-03-多种搜索

    1.query string search 2.query DSL 3.query filter 4.full-text search 5.phrase search 6.highlight sear ...

  2. Linux安装Nginx使用反向代理

    nginx的反向代理功能(自带了反向代理的功能,天生的二道贩子)1.实验环境准备准备2个服务器,都安装好nginx软件nginx1 192.168.13.79 作为web服务器 (理解为火车票售票点) ...

  3. pom.xml配置文件详解(转发)

    setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件:而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和li ...

  4. 添加启动项及常用Windows+R

    常用Windows+R services.msc---本地服务设置 msconfig---系统配置实用程序 mspaint--------画图板 notepad--------打开记事本 Nslook ...

  5. JSON JsonArray和JsonObject学习资料

    资料地址: http://www.json.org/json-zh.html

  6. P4103 [HEOI2014]大工程

    题目 P4103 [HEOI2014]大工程 化简题目:在树上选定\(k\)个点,求两两路径和,最大的一组路径,最小的一组路径 做法 关键点不多,建个虚树跑一边就好了 \(sum_i\)为\(i\)子 ...

  7. POJ 2230 Watchcow 【欧拉路】

    Watchcow Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6336   Accepted: 2743   Specia ...

  8. 格式化namenode时 报错 No Route to Host from node1/192.168.1.111 to node3:8485 failed on socket timeout exception: java.net.NoRouteToHostException: No route to host

    // :: FATAL namenode.NameNode: Failed to start namenode. org.apache.hadoop.hdfs.qjournal.client.Quor ...

  9. 大话设计模式--享元模式 Flyweight -- C++实现实例

    1. 享元模式: 运用共享技术有效地支持大量细粒度的对象. 享元模式可以避免大量非常相似类的开销,在程序设计中,有时需要生成大量颗粒度的类实例来表示数据,如果能发现这些实例除了几个参数外基本都是相同的 ...

  10. cscope usage

    1) Reference: Linux 平台下阅读源码的工具链 程序员的利器 – cscope 2) cscope help: :help cscope :help cscope-suggestion ...