POJ 1836 Alignment

Alignment
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 15867 Accepted: 5177

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

题意:给你一排士兵的身高,让你计算最少让几个士兵出对之后,剩下的所有士兵能够向左或者向右能够看到无穷远处。
其实是一个最长上升子序列的问题,先分别求出序列每个点的顺序和逆序的最长上升子序列长度,然后再枚举每两个点,使得
第一个点前面为升序,第二个点后面为降序即可,所求即为士兵总数减去枚举过的所有点里面升序序列人数加降序序列人数的最大值。
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

double a[11234];
int dp1[12345],dp2[12345];

int main()
{
    int n,s=0;  cin>>n;
    memset(dp1,0,sizeof(dp1));
    memset(dp2,0,sizeof(dp2));
    dp1[1]=dp2[n]=1;
    for(int i=1;i<=n;i++) scanf("%lf",&a[i]);
    for(int i=2;i<=n;i++)
    {
        int s=0;
        for(int j=1;j<i;j++) if(a[i]>a[j]&&dp1[j]>s) s=dp1[j];
        dp1[i]=s+1;
    }
    for(int i=n-1;i>=1;i--)
    {
        int s=0;
        for(int j=n;j>i;j--) if(a[i]>a[j]&&dp2[j]>s) s=dp2[j];
        dp2[i]=s+1;
    }
    for(int i=1;i<=n;i++)
    for(int j=i+1;j<=n;j++) s=max(s,dp1[i]+dp2[j]);
    cout<<n-s<<endl;
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值