题目

Description

In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight line in front of the captain. The captain is not satisfied with the way his soldiers are aligned; it is true that the soldiers are aligned in order by their code number: 1 , 2 , 3 , . . . , n , but they are not aligned by their height. The captain asks some soldiers to get out of the line, as the soldiers that remain in the line, without changing their places, but getting closer, to form a new line, where each soldier can see by looking lengthwise the line at least one of the line's extremity (left or right). A soldier see an extremity if there isn't any soldiers with a higher or equal height than his height between him and that extremity.

Write a program that, knowing the height of each soldier, determines the minimum number of soldiers which have to get out of line.

Input

On the first line of the input is written the number of the soldiers n. On the second line is written a series of n floating numbers with at most 5 digits precision and separated by a space character. The k-th number from this line represents the height of the soldier who has the code k (1 <= k > <= n).

There are some restrictions:
• 2 <= n <= 1000
• the height are floating numbers from the interval [0.5, 2.5]

Output

The only line of output will contain the number of the soldiers who have to get out of the line.

Sample Input

8
1.86 1.86 1.30621 2 1.4 1 1.97 2.2

Sample Output

4

题解

题意的大概意思就是删去数列中的一些数后,能够保证对于任一个数,其左边严格单调递减或者右边严格单调递增

因此删除后的数列中间两项是可以相等的

首先应该算出对于每一个数,从其向左的递减数列和向右的递增数列

也即,从两头分别算出最长上升子序列(dp[i]是在确保使用上i的情况下的最长上升子序列)

对于dp[i]

若a[i]>a[j]

  dp[i]=max{ dp[i] , dp[j]+1 }

否则

  dp[i]=max{ dp[i] , 1}

将dp初始化为1,则只需要判断a[i]>a[j]的内容即可

dp[i]=max{ dp[i] , dp[j]+1 }

最后枚举i、j求出dp1[i]+dp2[j]的最大值,即队伍中剩下的人数

用n减去得到答案

代码

/*
By:OhYee
Github:OhYee
HomePage:http://www.oyohyee.com
Email:oyohyee@oyohyee.com
Blog:http://www.cnblogs.com/ohyee/

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
using namespace std;

//DEBUG MODE
#define debug 0

//循环
#define REP(n) for(int o=0;o<n;o++)

const int maxn = 1005;
double num[maxn];
int ans[maxn];

int dp1[maxn];
int dp2[maxn];

//最长上升子序列
void LIS(double *a,int n,int *dp) {
    REP(n)
        dp[o] = 1;
    for(int i = 0;i < n;i++)
        for(int j = 0;j < i;j++)
            if(a[i] > a[j])
                dp[i] = max(dp[i],dp[j] + 1);
}
void LIS2(double *a,int n,int *dp) {
    REP(n)
        dp[o] = 1;
    for(int i = n-1;i >= 0;i--)
        for(int j = n-1;j > i;j--)
            if(a[i] > a[j])
                dp[i] = max(dp[i],dp[j] + 1);
}

bool Do() {
    int n;
    if(scanf("%d",&n) == EOF)
        return false;

    REP(n)
        scanf("%lf",&num[o]);

    //最小上升子序列
    /*
        dp[i]为前i个数能生成的最小上升子序列长度
        对于第i个数有两种情况:

            若a[i]>a[j]
                dp[i]=max{dp[j]+1,dp[i]}
            若a[i]<a[j]
                dp[i]=max{1,dp[i]}
                
            即dp[i] = max(dp[i],dp[j] + 1) (a[i]>a[j])
            初始为1
    */


    LIS(num,n,dp1);
    LIS2(num,n,dp2);

    /*
    int Max = -1;
    bool flag = false;
    REP(n) {
        ans[o] = dp1[o] + dp2[o];
        if(Max == ans[o] && ans[o-1] == Max) {
            flag = true;
        } else {
            if(Max < ans[o]) {
                Max = ans[o];
                flag = false;
            }
        }
    }
    */

    int ans2 = -1;
    for(int i = 0;i < n ;i++)
        for(int j = i + 1;j < n ;j++)
            ans2 = max(ans2,dp1[i] + dp2[j]);
    /*
    REP(n)
        printf("%d ",dp1[o]);
    printf("\n");
    REP(n)
        printf("%d ",dp2[o]);
    printf("\n");
    REP(n)
        printf("%d ",ans[o]);
    printf("\n");
    */

    //printf("%d\n",n - (Max - 1) - flag);
    printf("%d\n",n - ans2);

    return true;
}

int main() {
    while(Do());
    return 0;
}