poj-2533-LIS模板题
2017-07-24 15:59
267 查看
LongestOrderedSubsequence
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 |
#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;
}
相关文章推荐
- POJ - 2533 -Longest Ordered Subsequence(LIS模板题)
- POJ-2533-Longest Ordered Subsequence(LIS模板)
- poj 2533 && zoj 2136 Longest Ordered Subsequence --- LIS模板
- 【poj 2533】Longest Ordered Subsequence (LIS模板题)
- 【dp-LIS】牛客网 --最长上升子序列 POJ 2533--Longest Ordered Subsequence(LIS模板题)
- poj 3903 poj 2533 (LIS模板题)
- POJ 2533 LIS模板
- poj 2533 (LIS 最长递增子序列)
- Longest Increasing Subsequence[LIS 最长上升子序列问题] (Longest Ordered Subsequence) POJ - 2533 队列优化
- POJ 1631 LIS 模板题
- 最长上升子列 LIS 学习小记 Poj 2533 +CF 261D
- poj 2533(裸的LIS,很少见的水题)
- poj - 2533 - Longest Ordered Subsequence(LIS)
- POJ 2533 Longest Ordered Subsequence (LIS)
- LIS(n^2) POJ 2533 Longest Ordered Subsequence
- poj 2533 Longest Ordered Subsequence (LIS)
- POJ 2533 LIS
- POJ 2533 : Longest Ordered Subsequence - LIS问题
- POJ 2533 Longest Ordered Subsequence(dp LIS)
- POJ2533:Longest Ordered Subsequence(LIS)