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的不下降序列。程序要求,当原始数列给出后,求出最长的不下降数列的长度。
Sample Input
10
3 18 7 14 10 12 23 41 16 24
Sample Output
6
这题我们可以用逆推
设f[i]为从第i个到最后一个的最长不下降序列的长度
那我们就可以从f[i+1]到f[n]找到一个a[i]
var n,max,i,j:longint;
a,f:array[0..10000]of longint;
begin
readln(n);
for i:=1 to n do
begin
read(a[i]);
f[i]:=1;
end;
for i:=n-1 downto 1 do
begin
max:=1;
for j:=i+1 to n do
if (a[i]<a[j])and(f[j]+1>max) then max:=f[j]+1;
f[i]:=max;
end;
max:=0;
for i:=1 to n do
if max<f[i] then max:=f[i];
write(max);
end.