2533. Longest
Ordered Subsequence
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.
Input. The first line contains the length of sequence n (1 ≤ n ≤ 1000). The second line
contains the elements of sequence – n integers in the
range from 0 to 10000 each, separated by spaces.
Output. Output file
must contain a single integer – the length of the longest ordered subsequence of the given
sequence.
.
Sample
input |
Sample
output |
7 1 7 3 5 9 4 8 |
4 |
наибольшая
возрастающая подпоследовательность
В задаче следует
вычислить длину наибольшей возрастающей подпоследовательности.
Реализация алгоритма
#include <cstdio>
#include <algorithm>
#define MAX 1001
using namespace
std;
int x[MAX], lis[MAX];
int i, n, len, pos;
int main(void)
{
scanf("%d",&n);
for(i = 0; i
< n; i++) scanf("%d",&x[i]);
for (len = i
= 0; i < n; i++)
{
pos = lower_bound(lis,lis+len,x[i]) - lis;
if (pos < len) lis[pos] = x[i]; else lis[len++] = x[i];
}
printf("%d\n",len);
return 0;
}