POJ - 2533 Longest Ordered Subsequence(最长上升子序列)
d.最长上升子序列
s.注意是严格递增
c.O(nlogn)
#include<iostream>
#include<stdio.h>
using namespace std; const int MAXN=;
int a[MAXN],b[MAXN];
//b[k]是序列a中所有长度为k的递增子序列中的最小结尾元素值
//用二分查找的方法找到一个位置,使得num>b[i-1]并且num<b[i],并用num代替b[i]
int Search(int num,int low,int high){
int mid;
while(low<=high){
mid=(low+high)/;
if(num>=b[mid])low=mid+;
else high=mid-;
}
return low;
}
int DP(int n){
int i,len,pos;
b[]=a[];
len=;
for(i=;i<=n;i++){
if(a[i]>b[len]){//如果a[i]比b[]数组中最大还大直接插入到后面即可
len=len+;
b[len]=a[i];
}
else{//用二分的方法在b[]数组中找出第一个比a[i]大的位置并且让a[i]替代这个位置
pos=Search(a[i],,len);
b[pos]=a[i];
}
}
return len;
} int main(){ int N;
int i; while(~scanf("%d",&N)){
for(i=;i<=N;++i){
scanf("%d",&a[i]);
} printf("%d\n",DP(N));
} return ;
}
c2.O(n^2)
#include<iostream>
#include<stdio.h>
using namespace std; #define MAXN 1005 int a[MAXN];
int dp[MAXN];//dp[i]表示以a[i]作为结尾的最长上升子序列的长度 int main(){ int N;
int i,j;
int temp;
int ans; while(~scanf("%d",&N)){ for(i=;i<=N;++i){
scanf("%d",&a[i]);
} dp[]=; for(i=;i<=N;++i){ temp=; for(j=;j<i;++j){
if(a[i]>a[j]){
if(temp<dp[j]){
temp=dp[j];
}
}
} dp[i]=temp+;
} ans=;
for(i=;i<=N;++i){
if(dp[i]>ans){
ans=dp[i];
}
} printf("%d\n",ans);
} return ;
}
POJ - 2533 Longest Ordered Subsequence(最长上升子序列)的更多相关文章
- 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 Time Limit: 2000MS Memory Limit: 65536K Description A numeric se ...
- poj 2533 Longest Ordered Subsequence 最长递增子序列(LIS)
两种算法 1. O(n^2) #include<iostream> #include<cstdio> #include<cstring> using namesp ...
- POJ 2533 Longest Ordered Subsequence 最长递增序列
Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...
- 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与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 ...
- POJ 2533 Longest Ordered Subsequence(最长上升子序列(NlogN)
传送门 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subseque ...
- POJ 2533 Longest Ordered Subsequence(DP 最长上升子序列)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38980 Acc ...
- POJ 2533 Longest Ordered Subsequence(LIS模版题)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 47465 Acc ...
随机推荐
- Top JavaScript Frameworks, Libraries & Tools and When to Use Them
It seems almost every other week there is a new JavaScript library taking the web community by storm ...
- android数据存储之File
android中使用File进行存储主要使用到OpenFileOutput和OpenFileInput两个方法,下面直接用一个例子来说明一下. (1)布局文件main.xml文件 <?xml v ...
- 基于LBS的地理位置附近的搜索以及由近及远的排序
Nosql学习之Redis资料(一) http://redis.io/download 目前基于LBS地理位置的搜索已经应用非常广了,的确是个很方便的东西. 我们做程序的就是要考虑如何通过这些功能,来 ...
- 股票自用指标 boll 菜刀
BI:=(H+L+O+C)/; BOL:EMA(BI,N); UPPER:BOLL+N1*STD(CLOSE,N); LOWER:BOLL-N1*STD(CLOSE,N); MA1:MA(CLOSE, ...
- 【python】filter,map,reduce和lambda函数介绍
filter(function, iterable)map(function, iterable)reduce(function, sequence) filter将 function依次作用于ite ...
- LintCode "4 Sum"
4 Pointer solution. Key: when moving pointers, we skip duplicated ones. Ref: https://github.com/xbz/ ...
- 解读Nodejs多核处理模块cluster
来源: http://blog.fens.me/nodejs-core-cluster/ 从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发. ...
- Linux下Wi-Fi的实现:wireless_tools和wpa_supplicant
平台为hi35XX,在Liunx下借助wireless_tools和wpa_supplicant(因为现在的无线WIFI网络大多是wpa加密.所以需要移植wpa_supplicant工具)等工具实现w ...
- 黄聪:MYSQL提交一批ID,查询数据库中不存在的ID并返回
假设你数据库有个A表: ID NAME 1 aaa 2 bbb 3 ccc 4 ddd 需求:给你几个ID,返回A表中不存在的ID? 例如提交1,2,8,9 返回8,9 sel ...
- PLSQL_性能优化系列03_Oracle Parallel并发处理
2014-09-25 Created By BaoXinjian