求最长不下降序列:逆推法
2017-02-25 10:53
155 查看
题意
当原始数列给出后,求出最长的不下降数列的长度。
分析
f[i]表示第i数为起点到第n个数的最长不下降长度(倒推法)。
F[i]= max{1, F[j] + 1} (j = i+1…n, 且A[i] < A[j])。
F
=1;
最后一个循环,找出f[i]中最大的那一个。
var
max,n,i,j,l:longint;
f,a:array[1..2000]of longint;
begin
readln(n);
for i:=1 to n do
read(a[i]);
f
:=1;
for i:=n-1 downto 1 do
begin
l:=1;
for j:=i+1 to n do
if (a[i]<a[j])and(f[j]+1>l) then l:=f[j]+1;
f[i]:=l;
end;
l:=0;
for i:=1 to n do
if f[i]>l then l:=f[i];
write(l);
end.
当原始数列给出后,求出最长的不下降数列的长度。
分析
f[i]表示第i数为起点到第n个数的最长不下降长度(倒推法)。
F[i]= max{1, F[j] + 1} (j = i+1…n, 且A[i] < A[j])。
F
=1;
最后一个循环,找出f[i]中最大的那一个。
var
max,n,i,j,l:longint;
f,a:array[1..2000]of longint;
begin
readln(n);
for i:=1 to n do
read(a[i]);
f
:=1;
for i:=n-1 downto 1 do
begin
l:=1;
for j:=i+1 to n do
if (a[i]<a[j])and(f[j]+1>l) then l:=f[j]+1;
f[i]:=l;
end;
l:=0;
for i:=1 to n do
if f[i]>l then l:=f[i];
write(l);
end.
相关文章推荐
- 动态规划实现最大连续子序列和,最长不下降子序列和最长公共子序列
- POJ - 1952 BUY LOW, BUY LOWER(最长下降子序列,计数)
- 求最长上升/下降子序列【O(nlgn)】
- 拦截导弹(规律:下降子序列的个数等于最长上升子序列的长度)
- 【最长下降子序列】北大 poj 1952 BUY LOW, BUY LOWER
- 最长不下降子序列(动态规划:LIS)
- 最长不下降序列(HNOI’97)
- 最长不下降子序列的o(nlogn)算法
- 最长不下降子序列(O(N^2))
- 如何用O(nlogn)的的速度求解最长上升(下降)子序列
- BZOJ 1046 DP 逆求最长下降序列+枚举
- POJ-1952 最长下降子序列 + 方案数
- nlogn的最长不下降子序列【tyvj1254挑选士兵】
- 洛谷P2766 最长不下降子序列问题(最大流)
- DP 求最长不下降序列
- POJ1088(二维最长下降序列?)(我觉得是搜索)
- 最长下降子序列 nyoj 79
- 最长不下降子序列 nlogn
- 最长不下降字序列
- 蓝桥杯算法训练拦截导弹【最长上升子序列 & 最长非下降子序列nlogn 和 n^2】