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;
    }
     
  • 相关阅读:
    html实现虚线分隔线3例
    sql server 中将由逗号“,”分割的一个字符串,转换为一个表,并应用与 in 条件
    在Repeater中添加runat="server"的div,并控制
    Oracle中实现自增长列(转)
    SQL中按月进行分组(转)
    复制到剪切板js代码(转)
    如何使 Menu控件的链接打开指定的框架页(转)
    RadioButtonList限制每行"项"的个数
    Repeater_ItemDataBound 中用字段名获取值
    IIS7.0下配置Asp项目
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3108402.html
Copyright © 2011-2022 走看看