最长上升子序列 POJ 2533
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 47004 | Accepted: 20885 |
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
Source
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int i,j,a[n],b[n],m=0;
for(i=0;i<n;i++)
cin>>a[i];
b[0]=1;
for(i=1;i<n;i++)
{
b[i]=1;
for(j=0;j<i;j++)
{
if(a[i]>a[j] && b[j]+1>b[i])
b[i]=b[j]+1;
}
}
for(i=0;i<n;i++)
{
if(b[i]>m)
m=b[i];
}
cout<<m<<endl;
return 0;
}
O(nlogn)
<pre name="code" class="cpp">#include <iostream>
using namespace std;
int dp[10001];
int main()
{
int n;
cin>>n;
dp[0]=0;
int tail=0,a;
for(int i=1;i<=n;i++)
{
cin>>a;
//二分
int low=0,high=tail-1;
while(low<=high)
{
int mid=(low+high)/2;
if(dp[mid]>=a)
high=mid-1;
else low=mid+1;
}
if(low>=tail)
tail++;
dp[low]=a;
}
cout<<tail<<endl;
return 0;
}
.5kkdqdezd7g0.png)
