zoukankan      html  css  js  c++  java
  • Alignment--POJ1836

    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

    题意是n个士兵站队,最终使得每个士兵的左边或者右边的人的身高都是一次下降的,求的是出队的最小人数即总人数减去最多剩余的人数;
    可以先求出从左到右的递增子序列和从右到左的递增子序列;


    代码如下:

    #include<iostream>
    #include<stdio.h>
    #include<cstring>
    using namespace std;
    #define MAXN 1010
    double s[MAXN];
    int a[MAXN],b[MAXN];
    int n;
    int main()
    {
        while(cin>>n)
        {
            int i,j;
            for(i=1; i<=n; i++)
                scanf("%lf",&s[i]);
            a[1]=1;
            for(i=2; i<=n; i++) //从头开始计算最长的递增序列
            {
                a[i]=1;
                for(j=1; j<i; j++)
                {
                    if(s[i]>s[j]&&a[j]>=a[i])
                        a[i]=a[j]+1;
                }
            }
            b[n]=1;
            for(i=n-1; i>=1; i--) //从尾部计算最长的递增序列
            {
                b[i]=1;
                for(j=n; j>i; j--)
                {
                    if(s[i]>s[j]&&b[j]>=b[i])
                        b[i]=b[j]+1;
                }
            }
            int maxn=-100000;//初始化max
            for(i=1; i<=n; i++)
            {
                for(j=i+1; j<=n; j++)
                {
                    if(a[i]+b[j]>maxn)
                        maxn=a[i]+b[j];
                }
            }
            printf("%d
    ",n-maxn);
        }
        return 0;
    }
  • 相关阅读:
    如何分配资源和管理资源
    让Project把周六和周日也计算工期
    Project设置子任务
    pytest-配置文件
    pytest-fixture
    pytest-标记
    pytest-断言
    pytest-参数化
    pytest入门
    maven-插件
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/4465606.html
Copyright © 2011-2022 走看看