Longest Ordered Subsequence
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 47465   Accepted: 21120

Description

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1a2, ..., aN) be any sequence (ai1ai2, ..., 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

题目链接:POJ 2533

LIS模版题,N2和N*logN两种写法

N2代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1e3+10;
int arr[N],mx[N];
void init()
{
CLR(arr,0);
CLR(mx,0);
}
int main(void)
{
int n,i,j,pre_len;
while (~scanf("%d",&n))
{
init();
for (i=1; i<=n; ++i)
scanf("%d",&arr[i]);
mx[1]=1;
for (i=2; i<=n; ++i)
{
pre_len=0;
for (j=1; j<i; ++j)
{
if(arr[j]<arr[i])//arr[i]可以接到arr[j]后面
if(mx[j]>pre_len)//接到一个具有最长LIS的后面。
pre_len=mx[j];
}
mx[i]=pre_len+1;
}
printf("%d\n",*max_element(mx+1,mx+1+n));
}
return 0;
}

NlogN代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1e3+10;
int arr[N],d[N];
void init()
{
CLR(arr,0);
CLR(d,0);
}
int main(void)
{
int n,i,j,mxlen;
while (~scanf("%d",&n))
{
init();
for (i=1; i<=n; ++i)
scanf("%d",&arr[i]); mxlen=1;
d[mxlen]=arr[mxlen]; for (i=2; i<=n; ++i)
{
if(d[mxlen]<arr[i])
d[++mxlen]=arr[i];//最好情况一直往后增长
else
{
int pos=lower_bound(d,d+mxlen,arr[i])-d;//用二分找到一个下界可放置位置
d[pos]=arr[i];
}
}
printf("%d\n",mxlen);
}
return 0;
}

POJ 2533 Longest Ordered Subsequence(LIS模版题)的更多相关文章

  1. poj 2533 Longest Ordered Subsequence(LIS)

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

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

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

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

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

  4. poj 2533 Longest Ordered Subsequence 最长递增子序列

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...

  5. POJ 2533 Longest Ordered Subsequence(裸LIS)

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

  6. POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]

    题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric se ...

  7. Poj 2533 Longest Ordered Subsequence(LIS)

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

  8. POJ - 2533 Longest Ordered Subsequence与HDU - 1257 最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)(LIS)

    Longest Ordered Subsequence A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let ...

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

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

随机推荐

  1. Java删除文件夹和文件

    转载自:http://blog.163.com/wu_huiqiang@126/blog/static/3718162320091022103144516/ 以前在javaeye看到过关于Java操作 ...

  2. Struts2 Action与Servlet API耦合

    单元测试在开发中是非常重要的一个环节程序员在写完代码时,相应的单元测试也应写完整,否则你的代码就是不能让人信服的Struts2将Action与Servlet的API进行解耦之后,就使得单元测试变得非常 ...

  3. C#学习笔记-----基于AppDomain的"插件式"开发

    很多时候,我们都想使用(开发)USB式(热插拔)的应用,例如,开发一个WinForm应用,并且这个WinForm应用能允许开发人员定制扩展插件,又例如,我们可能维护着一个WinService管理系统, ...

  4. JS 正则表达式用法

    JS 正则表达式用法简介 简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具.其作用如下: 测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或 ...

  5. spring classpath & classpath*

    classpath-找到系统类路径下的第一个匹配的配置文件 classpath*-找到系统类路径下的所有符合要求的配置文件 参考资料:http://www.micmiu.com/j2ee/spring ...

  6. C#学习笔记(二)——变量和表达式

    Ps:使用这两个关键字可以很方便的把头文件收起来(虽然VS已经集成这个功能= =) 但是可以一下子收起来很多个函数 一.变量 1.简单类型 (1)变量类型 (2)示例一 static void Mai ...

  7. SQLServer2005利用维护计划自动备份数据库

    经常性忘了给数据库备份,结果当数据库发生问题的时候,才发现备份是1个月以前的,那个后悔与懊恼还加惭愧啊,别提有对难受了.要认为的记住去备份比较难,每天事情又那么多,所以有了这个自动备份就不用愁了.先拷 ...

  8. Hue中给BI分配的权限

    请保留hive的查询权限. 这个权限并不是分配给某个账户,而是分配给用户组.然后再将用户分入用户组中.

  9. php升级5.3到5.4,5.5,5.6

    Laravel要求php大于5.5.9,升级php5.3.3到5.6.14(最新版为 5.6.15 ) Add EPEL and Remi repositories onto your system: ...

  10. 2016.6.21 PHP与MqSQL交互之图片读取

    <td width="265"> <?php mysql_select_db("member"); mysql_query("set ...