首页 > 学院 > 开发设计 > 正文

求最长不下降序列-SSL 1459

2019-11-06 07:59:48
字体:
来源:转载
供稿:网友
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的不下降序列。程序要求,当原始数列给出后,求出最长的不下降数列的长度。 InputOutputSample Input103 18 7 14 10 12 23 41 16 24Sample Output6题解: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.
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表