您的位置:首页 > 其它

求最长不下降序列

2017-02-28 17:09 204 查看
求最长不下降序列
Time Limit:1000MS  Memory Limit:65536K

Total Submit:402 Accepted:174
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


逆推方法如下:

var

 map:array[1..1000] of longint;

 dis:array[1..1000] of longint;

 i,n,j,max:longint;

begin

 readln(n);

 for i:=1 to n do

  read(map[i]);

 dis
:=1;

 for i:=n-1 downto 1 do

  begin

  max:=1;

   for j:=i+1 to n do

   begin

    if (map[i]<map[j]) and (dis[j]+1>max) then

                                          max:=dis[j]+1;

    dis[i]:=max;

   end;

  end;

  max:=0;

  for i:=1 to n do

   if dis[i]>max then max:=dis[i];

  write(max);

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: