题解 | #合唱队#
合唱队
https://www.nowcoder.com/practice/6d9d69e3898f45169a441632b325c7b4
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
test24();
}
private static void test24() {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
int n = Integer.parseInt(in.nextLine());
String[] strs = in.nextLine().split(" ");
int[] left = new int[n];
for (int i = 0; i <= n - 1; i++) {
left[i] = 1;
for (int j = 0; j < i; j++) {
if (Integer.parseInt(strs[j]) < Integer.parseInt(strs[i])) {
left[i] = Math.max(left[i], left[j] + 1);
}
}
}
int[] right = new int[n];
for (int i = n - 1; i >= 0; i--) {
right[i] = 1;
for (int j = n - 1; j > i; j--) {
if (Integer.parseInt(strs[j]) < Integer.parseInt(strs[i])) {
right[i] = Math.max(right[i], right[j] + 1);
}
}
}
int v = 1;
for (int i = 0; i < n; i++) {
v = Math.max(left[i] + right[i] - 1, v);
}
System.out.println(n - v);
}
}
}
查看12道真题和解析