题解 | #Redraiment的走法#
Redraiment的走法
https://www.nowcoder.com/practice/24e6243b9f0446b081b1d6d32f2aa3aa
import java.util.*; public class Main { public static void main(String args[]) { Scanner scan = new Scanner(System.in); while (scan.hasNext()) { int n = scan.nextInt(); int [] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = scan.nextInt(); } int [] maxl = new int[n];//存储到各点的最长步数 int [] maxa = new int[n];//存储各最长步数对应的最大梅花桩高度 maxl[0] = 1; maxa[0] = a[0]; for (int i = 1 ; i < n; i++) { int index = i; int maxlen = 1; for (int j = i - 1; j >=0; j--) { if (a[i] > maxa[j]) {//a[i]的值动态更新,不要把状态值都压缩到maxl[i]而后续i+1处直接通过maxl[i]计算;应该通过多个状态值maxl[0]-maxl[i-1]计算maxl[i],即人人为我;新的参数a[i],我为人人,a[i]用于参与maxl[0]-maxl[i]的运算 if (maxl[j] + 1 > maxlen) { index = j; maxlen = maxl[j] + 1; } } } maxl[i] = maxl[index] + 1; maxa[i] = a[i]; } int maxlen = 0; for (int i = 0 ; i < a.length; i++) { if (maxl[i] > maxlen) { maxlen = maxl[i]; } } System.out.println(maxlen); } } }