一、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. [luogu3767]膜法

    [luogu3767]膜法 luogu 神仙题 线段树分治+带权并查集 把每个操作看成点 首先这个操作的结构是一棵树 你发现每个点的对它的子树产生影响 我们可以想到用dfn序把它转成一段区间用线段树分 ...

  2. SDOI2017第一轮

    本蒟蒻表示终于$AC$了$SDOI2017\text{第一轮}$! 兴奋! 附上各个题的题解: $DAT1$: $T1$: BZOJ4816: [Sdoi2017]数字表格 $T2$: BZOJ481 ...

  3. Asp.Net 5 Web Hook

    首先,然我们来看一下WebHooks是什么.WebHooks是一个协议.它们是HTTP回调技术.并且它们是"用户定义的HTTP回调".你和 (或) 您的应用程序在有什么事情发生时会 ...

  4. Django 动态导入配置文件的类

    导入配置文件的类 #如何用字符串的形式动态导入模块 from importlib import import_module path=''api.cors.CORSMiddleware' #CORSM ...

  5. 基于flask的web微信

    web微信 1.扫码获取头像 当你打开web微信的时候,因为http是无状态的,web微信如何实时的获取用户的扫码动作? 那么这里用到的是长轮询的方式. from flask import Flask ...

  6. SAP 物料 移动类型

    [转自 http://blog.sina.com.cn/s/blog_494f9a6b0102edf7.html] SAP 物料 移动类型 (2013-12-03 10:15:01) 转载▼   分类 ...

  7. memcached 不同客户端的问题

    摘要: memcached-java客户端调用get方法获取数据失败 主要演示一下在memcached服务器端set数据之后,在客户端调用java api获取数据.不过此过程如果不慎会读取数据失败. ...

  8. 《程序员代码面试指南》第三章 二叉树问题 判断t1 树中是否有与t2 树拓扑结构完全相同的子树

    题目 判断t1 树中是否有与t2 树拓扑结构完全相同的子树 java代码 package com.lizhouwei.chapter3; /** * @Description: 判断t1 树中是否有与 ...

  9. 0521 HTML基础

    一.web标准 web准备介绍: w3c:万维网联盟组织,用来制定web标准的机构(组织) web标准:制作网页遵循的规范 web准备规范的分类:结构标准.表现标准.行为标准. 结构:html.表示: ...

  10. 第四篇、javascript

    一.正则表达式 提示:此专题需要多轮复习反复的加深和理解 正则表达式的两种用法: 1)regexp.xxx(string); 2)string.yyy(regexp); 验证用户输入的手机号格式是否合 ...