题解 | #合唱队#
合唱队
https://www.nowcoder.com/practice/6d9d69e3898f45169a441632b325c7b4
import java.io.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { int count = Integer.valueOf(br.readLine()); String[] str = br.readLine().split(" "); int[] dp1 = new int[count]; int[] dp2 = new int[count]; int max1=0; int max2=0; for (int i = 0; i < count; i++) { dp1[i]=1; for (int j = 0; j < i; j++) { if(Integer.valueOf(str[i]) > Integer.valueOf(str[j])){ dp1[i]=Math.max(dp1[i],dp1[j]+1); } } max1=Math.max(dp1[i],max1); } for (int i = count-1; i >= 0; i--) { dp2[i]=1; for (int j = count-1; j > i; j--) { if(Integer.valueOf(str[i]) > Integer.valueOf(str[j])){ dp2[i]=Math.max(dp2[i],dp2[j]+1); } } max2=Math.max(dp2[i],max2); } int max3=0; for (int i = 0; i < count; i++) { max3 = Math.max(dp1[i]+dp2[i]-1,max3); } System.out.println(count-max3); } catch (IOException e) { e.printStackTrace(); } } }