题解 | #合唱队#
合唱队
https://www.nowcoder.com/practice/6d9d69e3898f45169a441632b325c7b4
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); String a, b; char[] chs; int i = 0, j = 0, n = 0, choir; try { a = r.readLine(); chs = a.toCharArray(); while (i < chs.length) { n = n * 10; n = n + chs[i] - '0'; i++; } b = r.readLine(); } catch (IOException e) { throw new RuntimeException(e); } int[] height = new int[n]; int[] numLt = new int[n]; int[] numRt = new int[n]; i = 0; n = 0; chs = b.toCharArray(); while (i < chs.length) { if (chs[i] == ' ') { height[j++] = n; n = 0; i++; continue; } n = n * 10; n = n + chs[i] - '0'; if (i == chs.length - 1) height[j] = n;//将身高数组赋值 i++; } i = 0; while (i < numLt.length) {//赋初始值1 numLt[i] = 1; numRt[i] = 1; i++; } i = 1; while (i < numLt.length) {//从左到右,左边比该位小的队列 j = 0; while (j < i) { if (height[j] < height[i] && numLt[j] + 1 > numLt[i]) numLt[i] = numLt[j] + 1; j++; } i++; } i = numRt.length - 2; while (i >= 0) {//从右到左,右边比该位小的队列 j = numRt.length - 1; while (j > i) { if (height[j] < height[i] && numRt[j] + 1 > numRt[i]) numRt[i] = numRt[j] + 1; j--; } i--; } i = 0; n = 0;//表示合唱队形最大的人数,左边递增加右边递减,然后减去自身 while (i < numLt.length) { choir = numLt[i] + numRt[i] - 1; //合唱队人数 if (choir > n) n = choir; i++; } System.out.print(height.length - n); } }