Description
设有n(n<=1000)个不相同的整数(小于32767)组成的数列,记为:
a1,a2,...,an,其中任意两个数不相同。
例如:3,18,7,14,10,12,23,41,16,24。
若有 且有 。则称为长度为e的不下降序列。如上例中,3,18,23,24为一个长度为4的不下降序列,同时也有3,7,10,12,16,24长度为6的不下降序列。程序要求,当原始数列给出后,求出最长的不下降数列的长度。
Input
Output
Sample Input
10
3 18 7 14 10 12 23 41 16 24
Sample Output
6
题解:b[i]表示第i个数到n个数的最长不下降长度。
F[i] = max{1, F[j] + 1} (j = i+1…n, 且A[i] < A[j])。
F[n]=1;
var
n,i,j,t:longint;
a,b:array[1..2000]of longint;
begin
readln(n);
for i:=1 to n do
read(a[i]);
b[n]:=1;
for i:=n-1 downto 1 do
begin
t:=1;
for j:=i+1 to n do
if (a[i]<a[j])and(b[j]+1>t) then t:=b[j]+1;
b[i]:=t;
end;
t:=0;
for i:=1 to n do
if b[i]>t then t:=b[i];
write(t);
end.
求最长不下降序列-SSL 1459
最新推荐文章于 2018-09-22 15:55:52 发布