题解 | #合唱队形#正反两次最长递增字序列
合唱队形
https://www.nowcoder.com/practice/cf209ca9ac994015b8caf5bf2cae5c98
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] nums1 = new int[n]; int[] nums2 = new int[n]; for (int i = 0; i < n; i++) { nums1[i] = sc.nextInt(); nums2[n - 1 - i] = nums1[i]; } int[] dp1 = new int[n]; int[] dp2 = new int[n]; for (int i = 0; i < n; i++) { dp1[i] = 1; dp2[i] = 1; for (int j = 0; j < i; j++) { if (nums1[j] < nums1[i] && dp1[j] + 1 > dp1[i]) { dp1[i] = dp1[j] + 1; } if (nums2[j] < nums2[i] && dp2[j] + 1 > dp2[i]) { dp2[i] = dp2[j] + 1; } } } int result = 0; for (int i = 0; i < n; i++) { if (dp1[i] + dp2[n - 1 - i] > result) { result = dp1[i] + dp2[n - 1 - i]; } } System.out.println(n - result + 1); } }