最长上升子序列
import java.util.*;
public class Main {
static int[] solution(int[] a, int n) {
int[] f = new int[n];
for(int i=0; i < n; i++) {
f[i] = 1;
for(int k =0; k < i; k++) {
if(a[k] < a[i])
f[i] = Math.max(f[i], f[k] + 1);
}
}
int[] maxn = new int[n];
maxn[0] = f[0];
for(int i=1; i < n; i++)
maxn[i] = Math.max(maxn[i-1], f[i]);
return maxn;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int[] a = new int[n];
int[] b = new int[n];
for(int i=0; i < n; i++) a[i] = sc.nextInt();
int[] len1 = solution(a, n);
for(int i=n-1; i >= 0; i--) b[i] = a[n-i-1];
int[] len2 = solution(b, n);
//System.out.println(Arrays.toString(len1));
int res = 0;
for(int i=0; i < n; i++) {
int t = len1[i];
t += len2[n-i-1];
t --;
res = Math.max(res, t);
}
System.out.println(n-res);
}
}
}
/*
以a[i]为分界,找左边最长递增子序列和右边最长递增子序列和最大,为队列最大长度。
f[i] 表示以i结尾,最长上升子序列
f[i] = max(f[k] + 1) , k < i, a[i] > a[k]
*/