您的位置:首页 > 其它

poj-2533-LIS模板题

2017-07-24 15:59 267 查看
LongestOrderedSubsequence

TimeLimit: 2000MS MemoryLimit: 65536K
TotalSubmissions: 53144 Accepted: 23696
Description

Anumericsequenceof ai isorderedif a1 < a2 <...< aN.Letthesubsequenceofthegivennumericsequence(a1, a2,..., aN)
beanysequence(ai1, ai2,..., aiK),where1<= i1 < i2 <...< iK <= N.Forexample,sequence
(1,7,3,5,9,4,8)hasorderedsubsequences,e.g.,(1,7),(3,4,8)andmanyothers.Alllongestorderedsubsequencesareoflength4,e.g.,(1,3,5,8).

Yourprogram,whengiventhenumericsequence,mustfindthelengthofitslongestorderedsubsequence.
Input

ThefirstlineofinputfilecontainsthelengthofsequenceN.Thesecondlinecontainstheelementsofsequence-Nintegersintherangefrom0to10000each,separatedbyspaces.1<=N<=1000
Output

Outputfilemustcontainasingleinteger-thelengthofthelongestorderedsubsequenceofthegivensequence.
SampleInput
7
1735948

SampleOutput
4

Source

NortheasternEurope2002,Far-EasternSubregion
方法一:O(n2)法

#include<iostream>
#include<algorithm>
usingnamespacestd;
#defineMAX100000

intn,A[MAX+1],L[MAX+1];//A[]含有输入数据,L[i]代表以i结尾的递增序列的长度
intlis()
{
intres=1;
for(inti=0;i<n;i++)//全部初始化为1
L[i]=1;
for(inti=1;i<n;i++)
{
for(intj=0;j<i;j++)
{
if(A[i]>A[j]&&L[i]<L[j]+1)//如果前面的数字小,且其对应长度没有前面那个数字对应长度长
L[i]=L[j]+1;
}
res=max(res,L[i]);
}
returnres;
}
intmain()
{
cin>>n;
for(inti=0;i<n;i++)
{
cin>>A[i];
}
cout<<lis()<<endl;
return0;
}方法二:O(nlogn)法

#include<iostream>
#include<algorithm>
usingnamespacestd;
#defineMAX100000

intn,A[MAX+1],L[MAX+1];
intlis()
{
L[0]=A[0];
intlength=1;
for(inti=1;i<n;i++)
{
if(L[length-1]<A[i])//如果末尾元素比当前元素小,序列加长
L[length++]=A[i];
else //如果末尾元素大,则序列中按大小插入替换当前元素,序列长度不变
*lower_bound(L,L+length,A[i])=A[i];
}
returnlength;
}
intmain()
{
cin>>n;
for(inti=0;i<n;i++)
{
cin>>A[i];
}
cout<<lis()<<endl;
return0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: