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. Ajax中的XMLHttpRequest对象详解

    XMLHttpRequest对象是Ajax技术的核心.在Internet Explorer 5中,XMLHttpRequest对象以ActiveX对象引入,被称之为XMLHTTP,它是一种支持异步请求 ...

  2. 自定义view实现水波纹效果

    水波纹效果: 1.标准正余弦水波纹: 2.非标准圆形液柱水波纹: 虽说都是水波纹,但两者在实现上差异是比较大的,一个通过正余弦函数模拟水波纹效果,另外一个会运用到图像的混合模式(PorterDuffX ...

  3. Fresco 源码分析(一) DraweeView-DraweeHierarchy-DraweeController(MVC) DraweeView的分析

    4. Fresco的内容 为了方便学习,我们先从使用结合官方的文档来分析 4.1 Fresco客户端的使用 在使用Fresco的使用,我们直接使用的是SimpleDraweeView这个类,然后在Ac ...

  4. Java Hour 53 HQL

    上回写到一个一个最基本的HQL 查询语句写出来都没有什么自信,这一课时就补上HQL 相关的知识. 这种东西笔者最喜欢的官方的原版说明文档了. http://docs.jboss.org/hiberna ...

  5. js event 2

    js event 2  浏览器兼容 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  6. THINKPHP 默认模板路径替换

    APP_PATH // 当前项目目录APP_NAME // 当前项目名称 ACTION_NAME // 当前操作名称 CACHE_PATH // 项目模版缓存目录 CONFIG_PATH //项目配置 ...

  7. 开发Android 范的错误

    1 在onCreate(Bundle savedInstanceState)方法中, 按钮单击事件的实现直接写在onCreate方法了里,这样就好导致这个按钮只能触发一次, 因为在Android体系中 ...

  8. POJ2735/Gym 100650E Reliable Nets dfs

    Problem E: Reliable NetsYou’re in charge of designing a campus network between buildings and are ver ...

  9. BroadcastRecevier广播接受者

    广播接收器的两种注册方式: 1)动态注册:在代码中注册,创建一个IntentFilter(意图过滤器)对象,设置想要就收的广播,在onCreate()方法中通过调用registerReceiver() ...

  10. GPS基础

    public class MainActivity extends Activity { private LocationManager manager; private List<String ...