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. ORACLE查询当前资产状态,和另一个数据库联查,(查询重复数据中第一条),子查询作为字段查询

    背景:ORACLE查询当前资产状态,包含资产信息(表1),资产维修状态(表2),资产报废状态(表3) 如下: 资产信息:

  2. AJAX,JSON搜索智能提示

    效果 开发结构参考AJAX,JSON用户校验 主要有两个核心文件 1,处理输入字符,进行后台搜索的servlet Suggest.java package org.guangsoft.servlet; ...

  3. 数列(codevs 1141)

    题目描述 Description 给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列,例如,当k=3时,这个序列是: 1,3,4,9,10,12,13 ...

  4. Android源码编译

    参考百度文章: http://jingyan.baidu.com/article/a501d80ce61ad0ec630f5e0b.html

  5. 解决Windows和Ubuntu时间不一致的问题

    问题原因是使用的时间不一致导致的.win10直接从bios读出来的时间认为就是实际时间,ubuntu认为加上8个小时 后的才是.win10用的rtc ,ubuntu用的utc 在ubuntu16.04 ...

  6. 我对序列化(Serializable)的理解

    转自:http://blog.tianya.cn/blogger/post_show.asp?BlogID=764&PostID=3231409 序列化是把一个对象的状态写入一个字节流的过程. ...

  7. MySQL监控系统MySQL MTOP的搭建

    MySQLMTOP是一个由Python+PHP开发的MySQL企业级监控系统.系统由Python实现多进程数据采集和告警,PHP实现WEB展示和管理.最重要是MySQL服务器无需安装任何Agent,只 ...

  8. win 8.1 安装framework3.5

    其实win8的iso上带着net 3.5的功能的,但是并没有随着系统安装二安装上去.所以只要你有跟你系统同个版本的iso文件,就可以实现不联网安装net3.5.首先把装载你的iso.Win8系统可以打 ...

  9. jmeter断言

    响应断言:检查的内容,如果是变量,设置为变量 断言结果:断言的结果 断言持续时间:请求事件不能超过设置的断言持续时间 Size Assertion:断言字节大小

  10. Java POI Word 写文档

    package apache.poi; import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import ...