又是站在巨人肩膀上的答案
其实就是最长上升子序列
原答案:代码届的小白
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = sc.nextInt(); } int[] left = new int[n]; int[] right = new int[n]; for(int i = 0; i < n; i++){ left[i] = 1; for(int j = 0; j < i; j++){ if(arr[i] > arr[j]) left[i] = Math.max(left[j] + 1, left[i]); } } for(int i = n - 1; i >= 0; i--){ right[i] = 1; for(int j = n - 1; j > i; j--){ if(arr[i] > arr[j]) right[i] = Math.max(right[j] + 1, right[i]); } } int[] res = new int[n]; for(int i = 0; i < n; i++){ res[i] = left[i] + right[i] - 1; } int max = 0; for(int i = 0; i < n; i++){ max = Math.max(max, res[i]); } System.out.println(n - max); } }