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.

Sample Input

7
1 7 3 5 9 4 8

Sample Output

4
题意:输出最长递增子序列的长度
思路:直接裸LIS,第一次使用,使用两种方法
 
第一种:复杂度n^2
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std; int a[1005],dp[1005],n; int LIS()
{
int i,j,ans,m;
dp[1] = 1;
ans = 1;
for(i = 2;i<=n;i++)
{
m = 0;
for(j = 1;j<i;j++)
{
if(dp[j]>m && a[j]<a[i])
m = dp[j];
}
dp[i] = m+1;
if(dp[i]>ans)
ans = dp[i];
}
return ans;
} int main()
{
int i;
while(~scanf("%d",&n))
{
for(i = 1;i<=n;i++)
scanf("%d",&a[i]);
printf("%d\n",LIS()); } return 0;
}

第二种:nlogn

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std; int a[1005],dp[1005],c[1005],n; int bin(int size,int k)
{
int l = 1,r = size;
while(l<=r)
{
int mid = (l+r)/2;
if(k>c[mid] && k<=c[mid+1])
return mid+1;
else if(k<c[mid])
r = mid-1;
else
l = mid+1;
}
} int LIS()
{
int i,j,ans=1;
c[1] = a[1];
dp[1] = 1;
for(i = 2; i<=n; i++)
{
if(a[i]<=c[1])
j = 1;
else if(a[i]>c[ans])
j = ++ans;
else
j = bin(ans,a[i]);
c[j] = a[i];
dp[i] = j;
}
return ans;
} int main()
{
int i;
while(~scanf("%d",&n))
{
for(i = 1; i<=n; i++)
scanf("%d",&a[i]);
printf("%d\n",LIS()); } return 0;
}
												

POJ2533:Longest Ordered Subsequence(LIS)的更多相关文章

  1. POJ2533:Longest Ordered Subsequence

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

  2. poj 2533 Longest Ordered Subsequence(LIS)

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

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

    本题大意:和LIS一样 本题思路:用dp[ i ]保存前 i 个数中的最长递增序列的长度,则可以得出状态转移方程dp[ i ] = max(dp[ j ] + 1)(j < i) 参考代码: # ...

  4. POJ 2533 Longest Ordered Subsequence LIS O(n*log(n))

    题目链接 最长上升子序列O(n*log(n))的做法,只能用于求长度不能求序列. #include <iostream> #include <algorithm> using ...

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

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

  6. (线性DP LIS)POJ2533 Longest Ordered Subsequence

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

  7. POJ2533 Longest Ordered Subsequence —— DP 最长上升子序列(LIS)

    题目链接:http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 6 ...

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

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

  9. POJ2533——Longest Ordered Subsequence(简单的DP)

    Longest Ordered Subsequence DescriptionA numeric sequence of ai is ordered if a1 < a2 < ... &l ...

随机推荐

  1. winPcap_1_开篇

    什么是WinPcap WinPcap是一个基于Win32平台的,用于捕获网络数据包并进行分析的开源库. 因为有些应用程序需要直接访问网络中的数据包.也就是说,那些应用程序需要访问原始数据包,即没有被操 ...

  2. Mysql学习(慕课学习笔记5)约束

    约束类型: 1.NOT NULL (非空约束) 2.PRIMARY KEY(主键约束) 每张数据表只能存在一个主键 主键保证记录的唯一性 主键自动为NOT NULL (Auto_increment  ...

  3. Google机器学习笔记(七)TF.Learn 手写文字识别

    转载请注明作者:梦里风林 Google Machine Learning Recipes 7 官方中文博客 - 视频地址 Github工程地址 https://github.com/ahangchen ...

  4. LoadRunner参数化功能详解

    更新方式: .      Each Occurrence 每次遇到参数就进行更新. 多次使用同一参数,而且没有什么关联,例如随机数. Each Iteration 每次迭代时发生更新. 如果参数出现几 ...

  5. 百度地图api实例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm11.aspx ...

  6. Android中读取assets文件夹中的子文件夹内容

    文件结构如下:assets/info/info AssetManager am = this.getResources().getAssets(); InputStream input = null; ...

  7. ADT 连接手机运行android应用程序时报错

    The connection to adb is down, and a severe error has occured.    You must restart adb and Eclipse.  ...

  8. 【转帖】C# DllImport 系统调用使用详解 托管代码的介绍 EntryPoint的使用

    1      DLLImport的使用 using System; using System.Runtime.InteropServices; //命名空间 class Example { //用Dl ...

  9. Java获取当前日期的前一个月,前一天的时间

    Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -); //得到前一天 calendar.add(Cal ...

  10. Roman to Integer && Integer to Roman 解答

    Roman Numeral Chart V:5 X:10 L:50 C:100 D:500 M:1000 规则: 1. 重复次数表示该数的倍数2. 右加左减:较大的罗马数字右边记上较小的罗马数字,表示 ...