poj1836Alignment(dp)

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


解题思路:POJ2533的扩展题。

题意不难,令到原队列的最少士兵出列后,使得新队列任意一个士兵都能看到左边或者右边的无穷远处。就是使新队列呈三角形分布就对了。

 

但这里有一个陷阱,看了一些别人的解题报告说“任一士兵旁边不能存在等高的士兵”,然后又举了一个例子说注意

3

5 5 5

的情况,我没看他们的程序,不知道他们是不是把这些例子特殊处理了,但完全没必要,因为“允许处于三角形顶部的两个士兵等高”,图形化就是如下图:

 

其实蓝色士兵的身高和红色士兵的身高是完全没有关系的。

 

要求最少出列数,就是留队士兵人数最大,如图,即左边的递增序列人数和右边的递减序列人数之和最大

因而可转化为求“最长不降子序列”和“最长不升子序列”问题

 

但是不能直接套用LIS思想,因为这题不允许任一侧的序列中出现等高士兵

 

基本操作方法:

对士兵的身高数组逐一进行枚举,枚举到的k值作为蓝色士兵,k+1值作为红色士兵,以这两个士兵分别作为最长不降子序列L1的终点和最长不升子序列L2的起点,即作为整个队列的分界点。

然后分别对两边进行dp,枚举到某一个m值时,使得L1+L2的长度为最大max,此时用总士兵人数n 减去max就是  最少出列人数
#include<stdio.h>
#include<string.h>
#define max(a,b) a>b?a:b
int main()
{
    int n;
    double p[1100];
    int l[1100],r[1100];
    int i,j,k;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%lf",&p[i]);
            l[i]=r[i]=1;
        }
        for(i=1;i<=n;i++)
        {
            int m=0;
            for(j=1;j<i;j++)
            {
                if(l[j]>m&&p[i]>p[j])
                {
                    m=l[j];
                }
            }
            l[i]=m+1;
           // printf("%d ",l[i]);
        }
          //printf("\n");
        for(i=n;i>=1;i--)
        {
            int m=0;
            for(j=n;j>i;j--)
            {
                if(r[j]>m&&p[i]>p[j])
                {
                    m=r[j];
                }
            }
            r[i]=m+1;

        }
        //for(i=1;i<=n;i++)
           // printf("%d ",r[i]);
        int maxn=0;
        for(i=1;i<n;i++)
        {
            for(j=i+1;j<=n;j++)
            maxn=max(maxn,(l[i]+r[j]));
        }

       printf("%d\n",n-maxn);

    }
    return 0;

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值