http://poj.org/problem?id=2533
Longest Ordered Subsequence
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 31847 | Accepted: 13934 |
Description
A numeric sequence of
ai is ordered if
a1 <
a2 < ... <
aN. Let the subsequence of the given numeric sequence (
a1,
a2, ...,
aN) be any sequence (
ai1,
ai2, ...,
aiK), where 1 <=
i1 <
i2 < ... <
iK <=
N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Input
The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000
Output
Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.
Sample Input
7 1 7 3 5 9 4 8
Sample Output
4
题目的大概意思就是要我们测出最长递增子序列的长度,意思很容易懂,我就不多说了,直接看代码吧!
AC代码:
#include<iostream>
#include<cstring>
using namespace std;
int a[1005],b[1005];//b数组用来存最长递增子序列。
int main()
{
int n,i,k,j;
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
k=1;//k用来记录最长递增子序列的长度。
b[k]=a[1];
for(i=2;i<=n;i++)
{
if(a[i]>b[k])//找到比a[i]大的存进b;
{
k+=1;
b[k]=a[i];
}
else
{
for(j=1;j<=k;j++)//判断b中是否有大于a[i]的。
if(b[j]>a[i])
{
b[j]=a[i];//找到b数组中第一个比a[i]大的数,将它覆盖,再跳出。
break;
}
}
}
printf("%d\n",k);
}
return 0;
}