题解 | #合唱队#
合唱队
http://www.nowcoder.com/practice/6d9d69e3898f45169a441632b325c7b4
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
int total = scan.nextInt();
int[] arr = new int[total+1];
for(int i = 1;i<=total;i++){
arr[i] = scan.nextInt();
}
int [] l =left(arr);
int [] r =right(arr);
int max = 0;
for(int i = 0;i<arr.length;i++){
if(max < (l[i]+r[i]-1)){
max = l[i]+r[i]-1;
}
}
System.out.println(total - max);
}
}
public static int []left(int[] arr){
int[]left = new int[arr.length];
for(int i = 1; i < arr.length ; i++){
left[i] = 1;
for(int j = 1 ; j <i ; j++){
if(arr[j] < arr[i]){
left[i] = Math.max(left[i],left[j]+1);
}
}
}
return left;
}
public static int []right(int[] arr){
int[]right = new int[arr.length];
for(int i = arr.length-1; i >0 ; i--){
right[i] = 1;
for(int j = arr.length-1 ; j >i ; j--){
if(arr[j] < arr[i]){
right[i] = Math.max(right[i],right[j]+1);
}
}
}
return right;
}
}
public class Main {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
int total = scan.nextInt();
int[] arr = new int[total+1];
for(int i = 1;i<=total;i++){
arr[i] = scan.nextInt();
}
int [] l =left(arr);
int [] r =right(arr);
int max = 0;
for(int i = 0;i<arr.length;i++){
if(max < (l[i]+r[i]-1)){
max = l[i]+r[i]-1;
}
}
System.out.println(total - max);
}
}
public static int []left(int[] arr){
int[]left = new int[arr.length];
for(int i = 1; i < arr.length ; i++){
left[i] = 1;
for(int j = 1 ; j <i ; j++){
if(arr[j] < arr[i]){
left[i] = Math.max(left[i],left[j]+1);
}
}
}
return left;
}
public static int []right(int[] arr){
int[]right = new int[arr.length];
for(int i = arr.length-1; i >0 ; i--){
right[i] = 1;
for(int j = arr.length-1 ; j >i ; j--){
if(arr[j] < arr[i]){
right[i] = Math.max(right[i],right[j]+1);
}
}
}
return right;
}
}