poj 2533 Longest Ordered Subsequence 最长递增子序列(LIS)
两种算法
1. O(n^2)
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int a[];
int dp[];
int main()
{
int n, maxn;
while(scanf("%d", &n) != EOF)
{
maxn = ;
for(int i = ; i < n; i++)
{
scanf("%d", &a[i]);
dp[i] = ;
for(int j = ; j < i; j++)
{
if(a[j] < a[i] && dp[j] + > dp[i])
dp[i] = dp[j] + ;
}
}
for(int i=;i<n;i++)
{
if(maxn < dp[i])
maxn = dp[i];
}
printf("%d\n", maxn);
}
return ;
}
2.O(nlog(n))
O(nlogn)的算法关键是它建立了一个数组c[],c[i]表示长度为i的不下降序列中结尾元素的最小值,用K表示数组目前的长度,算法完成后K的值即为最长不下降子序列的长度。
具体点来讲:
设当前的以求出的长度为K,则判断a[i]和c[k]:
1.如果a[i]>=c[k],即a[i]大于长度为K的序列中的最后一个元素,这样就可以使序列的长度增加1,即K=K+1,然后现在的c[k]=a[i];
2.如果a[i]<c[k],那么就在c[1]...c[k]中找到最大的j,使得c[j]<a[i],然后因为c[j]<a[i],所以a[i]大于长度为j的序列的最后一个元素,那么就可以更新长度为j+1的序列的最后一个元素,即c[j+1]=a[i]。
算法复杂度的分析:
因为共有n个元素要进行计算;每次计算又要查找n次,所以复杂度是O(n^2),但是,注意到c[]数组里的元素的单调递增的,所以我们可以用二分法,查找变成了logn次。这样算法的复杂度就变成了O(nlogn)。
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int a[],dp[],c[],n; int bin(int size,int k)
{
int l=,r=size;
while(l<=r)
{
int mid=(l+r)/;
if(k>c[mid]&&k<=c[mid+])
return mid+;
else if(k<c[mid])
r=mid-;
else
l=mid+;
} }
int LIS()
{
c[]=a[];
dp[]=;
int j,ans=;
for(int i=;i<=n;i++)
{
if(a[i]<=c[])
j=;
else if(a[i]>c[ans])
j=++ans;
else
j=bin(ans,a[i]);
c[j]=a[i];
dp[i]=j;
}
return ans;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
printf("%d\n",LIS());
}
return ;
}
poj 2533 Longest Ordered Subsequence 最长递增子序列(LIS)的更多相关文章
- 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 最长递增序列
Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- 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 ...
随机推荐
- H5教程:移动页面性能优化
随着移动互联网的发展,我们越发要关注移动页面的性能优化,今天跟大家谈谈这方面的事情. 首先,为什么要最移动页面进行优化? 纵观目前移动网络的现状,移动页面布局越来越复杂,效果越来越炫,直接导致了文件越 ...
- SKU和SPU表的设计
1.什么是SKU,SPU SKU:Stock Keeping Unit (库存量单位)SKU即库存进出计量的单位,可以是以件.盒.托盘等为单位,是物理上不可分割的最小存货单元.在使用时要根据不同业态, ...
- JSON.stringify(),JSON.parse(),eval(string)
JSON.stringify()用于从一个对象解析出字符串 : var obj = {"name":"week","age":" ...
- WebKit.NET-0.5简单应用(2)——音量解决方案
查找WebKit.NET相关文档,没有找到音量控制解决方法.换思路进行解决,尝试用Win32 API进行解决 [DllImport("winmm.dll")] public sta ...
- POJ——T 3687 Labeling Balls
http://poj.org/problem?id=3687 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14842 ...
- 洛谷 P3662 [USACO17FEB]Why Did the Cow Cross the Road II S
P3662 [USACO17FEB]Why Did the Cow Cross the Road II S 题目描述 The long road through Farmer John's farm ...
- Android笔记——Activity中的数据传递案例(用户注冊)
1.创建程序activity_main: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro ...
- Effective C++ 条款12
复制对象时,勿忘其每个成分 作者在本节条款提醒我们,在多重继承的情况下进行copy或者copy assignment 的operator=的编写时,一定要考虑base 类部分数据的初始化后者复制. 对 ...
- Getting Started with MongoDB (C# Edition)
https://docs.mongodb.com/getting-started/csharp/ 概览 Welcome to the Getting Started with MongoDB guid ...
- Clion远程开发
2018.3 开始Clion可以支持远程开发了 官网教程如下: https://www.jetbrains.com/help/clion/remote-projects-support.html ...