POJ 2533 Longest Ordered Subsequence(LIS模版题)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 47465 | Accepted: 21120 |
Description
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Input
Output
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模版题)的更多相关文章
- poj 2533 Longest Ordered Subsequence(LIS)
Description A numeric sequence of ai is ordered ifa1 <a2 < ... < aN. Let the subsequence of ...
- POJ-2533.Longest Ordered Subsequence (LIS模版题)
本题大意:和LIS一样 本题思路:用dp[ i ]保存前 i 个数中的最长递增序列的长度,则可以得出状态转移方程dp[ i ] = max(dp[ j ] + 1)(j < i) 参考代码: # ...
- POJ 2533 Longest Ordered Subsequence LIS O(n*log(n))
题目链接 最长上升子序列O(n*log(n))的做法,只能用于求长度不能求序列. #include <iostream> #include <algorithm> using ...
- poj 2533 Longest Ordered Subsequence 最长递增子序列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...
- POJ 2533 Longest Ordered Subsequence(裸LIS)
传送门: http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 6 ...
- POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]
题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric se ...
- Poj 2533 Longest Ordered Subsequence(LIS)
一.Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...
- POJ - 2533 Longest Ordered Subsequence与HDU - 1257 最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)(LIS)
Longest Ordered Subsequence A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let ...
- 题解报告:poj 2533 Longest Ordered Subsequence(最长上升子序列LIS)
Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence ...
随机推荐
- HDU2044 小蜜蜂斐波那契
一只小蜜蜂... Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDU1005&&NEFU67 没有循环节
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- SQLHelper、DBUtil终极封装
DBUtil.java package org.guangsoft.util; import java.io.InputStream; import java.sql.Connection; impo ...
- .pro配置选项
在Qt Creator的项目中添加头文件和库 在Qt Creator中的工程中,工程通过.pro文件管理. 额外需要连接的连接库 unix:LIBS += -L your_lib_path -ly ...
- php 面向对象的方式访问数据库
<body> <?php //面向对象的方式访问数据库 //造对象 $db = new MySQLi("localhost","root",& ...
- IIS网站服务器性能优化指南(转载)
原文网址:http://www.phontol.com/20090507_419416_1.html Windows Server自带的互联网信息服务器(Internet Informat ...
- 64位ubuntu安装32位jdk
转自:http://blog.csdn.net/anladeyatou/article/details/8213334 ubuntu-11.10-desktop-amd64 jdk-6u23-linu ...
- JSON详解(转)
JSON详解 JSON的全称是”JavaScript Object Notation”,意思是JavaScript对象表示法,它是一种基于文本,独立于语言的轻量级数据交换格式.XML也是一种数据交换格 ...
- loj 1011(状态压缩+记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25837 思路:状态压缩+记忆化搜索. #include<io ...
- poj 3140(树形dp)
题目链接:http://poj.org/problem?id=3140 思路:简单树形dp题,dp[u]表示以u为根的子树的人数和. #include<iostream> #include ...