zoukankan      html  css  js  c++  java
  • 求最长不下降序列:逆推法

    题意

    当原始数列给出后,求出最长的不下降数列的长度。 

    分析

    f[i]表示第i数为起点到第n个数的最长不下降长度(倒推法)。

    F[i]= max{1, F[j] + 1} (j = i+1…n, 且A[i] < A[j])。

    F[n]=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[n]:=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.

  • 相关阅读:
    微博Feed流
    朋友圈
    Go命令行—compile
    Practical Go: Real world advice for writing maintainable Go programs
    Nginx Cache-Control
    Redis 主从复制
    JAVA
    Tomcat
    Tomcat
    CentOS 7
  • 原文地址:https://www.cnblogs.com/YYC-0304/p/9500178.html
Copyright © 2011-2022 走看看