描述 Description | |||
求最长不下降子序列的长度 | |||
输入格式 Input Format | |||
第一行为n,表示n个数 第二行n个数 | |||
输出格式 Output Format | |||
最长不下降子序列的长度 | |||
样例输入 Sample Input | |||
样例输出 Sample Output | |||
时间限制 Time Limitation | |||
各个测试点1s | |||
注释 Hint | |||
N小于5000 for each num <=maxint |
经典DP,让我想到了合唱队形。
f[i]表示第i个人向左能得到的最大值,
枚举j from 1 to i-1
在a[i]>=a[j] 的条件下f[i]:=max[f[j]+1];
var
n,i,j,k:longint;
a,f:array[1..5000] of longint;
begin
readln(n);
for i:=1 to n do read(a[i]);
for i:=1 to n do f[i]:=1;
f[1]:=1;
for i:=2 to n do
for j:=1 to i-1 do
if (a[i]>=a[j]) and (f[i]<f[j]+1) then f[i]:=f[j]+1;
k:=0;
for i:=1 to n do
if f[i]>k then k:=f[i];
writeln(k);
end.
需要注意的是这句: for i:=1 to n do f[i]:=1; 赋初值的时候一定要赋成1,不能赋成0.。。不然要悲剧。。