zoukankan      html  css  js  c++  java
  • POJ 1836 Alignment

    Alignment
     
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 10672   Accepted: 3414

    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 <stdio.h>
    #include <iostream>
    using namespace std;
    
    void Find(double arr[], int n, int dp[])
    {
        int Max = -1;
        dp[0] = 1;
        for (int i = 1; i < n; i++)
        {
            Max = 0;
            dp[i] = 1;
            for (int j = 0; j < i; j++)
            {
                if (arr[i] > arr[j] && dp[j] + 1 > dp[i])
                {
                    dp[i] = dp[j] + 1;
                }
            }
        }
    }
    
    int main()
    {
        int n;
        int Max = -1;
        double a[1005];
        double b[1005];
        int dp[1005];
        int dp1[1005];
        scanf("%d", &n);
        dp[n] = dp1[n] = 0;
        for (int i = 0; i < n; i++)
        {
            scanf("%lf", &a[i]);
            b[n - 1 - i] = a[i];
        }
        Find(a, n, dp);
        Find(b, n, dp1);
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                if (dp[i] + dp1[n - j - 1] > Max)
                {
                    Max = dp[i] + dp1[n - j - 1];
                }
            }
        }
        printf("%d\n", n - Max);
        return 0;
    }
     
  • 相关阅读:
    python装饰器的作用
    python的__call__、__str__、__repr__、__init__、__class__、__name___、__all__、__doc__、__del__等魔术方法的作用
    安全小测试:介绍一个简单web安全知识测试的网站
    浏览器都知道我们的哪些信息?
    SQL开发技巧(二)
    如何解决SQLServer占CPU100%
    记一次SQLServer的分页优化兼谈谈使用Row_Number()分页存在的问题
    如何在SQLServer中处理每天四亿三千万记录
    SqlServer索引的原理与应用
    T-sql语句查询执行顺序
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3108402.html
Copyright © 2011-2022 走看看