zoukankan      html  css  js  c++  java
  • Alignment

    Problem 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
     
    Source
    PKU

    题意:从左到右的给出n个士兵的身高要求走出几个士兵让剩余的士兵能够看到左和右的至少一边的无穷远处;

    要求:求出应该出列的士兵的最少个数;

    思路:对于第i点;求0~i这个区间的最大上升子数列的元素个数和i+1~n-1这个区间的最大下降子序列的元素个数的和;求出这n个和的最大值即是所剩余的士兵的最大数量;

    用n减去这个数即是应该出列的士兵的最少个数;

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<string>
     5 #include<algorithm>
     6 #include<cmath>
     7 
     8 using namespace std;
     9 
    10 
    11 
    12 
    13 int main()
    14 {
    15     double s[1010]={0};
    16     int n;
    17     int dp[2][1010]={0};
    18     int i,j;
    19     int max=0;
    20     while(cin>>n)
    21     {
    22         max=0;
    23         memset(dp,0,sizeof(dp));
    24         for(i=1;i<=n;i++)
    25             cin>>s[i];//依次输入士兵的身高;
    26         for(i=1;i<=n;i++)//从左到右求最长上升子数列;
    27         {
    28             max=0;
    29             for(j=0;j<i;j++)
    30             {
    31                 if(dp[0][j]>max&&s[j]<s[i])max=dp[0][j];
    32             }
    33             dp[0][i]=max+1;
    34         }
    35         for(i=n;i>=0;i--)//从右向左求最长上升子数列;
    36         {
    37             max=0;
    38             for(j=i+1;j<=n;j++)
    39             {
    40                 if(dp[1][j]>max&&s[i]>s[j])max=dp[1][j];
    41             }
    42             dp[1][i]=max+1;
    43         }
    44         max=0;
    45         for(i=1;i<=n;i++)//对于第i个士兵,从左到第i处的最长子数列的元素个数
    46         {//从最右边开始到第i+1处最长子数列的元素个数
    47             for(j=i+1;j<=n;j++)//求以上两者的和的最大值;
    48             if(dp[0][i]+dp[1][j]>max)max=dp[0][i]+dp[1][j];
    49         }
    50         cout<<n-max<<endl;
    51     }
    52     return 0;
    53 }
    View Code
  • 相关阅读:
    控制语句
    (小程序)接收用户键盘输入
    运算符
    java doc形成注释文档
    linux中rz、sz命令,zip、unzip命令,sqlite3基本操作
    hash应用-加随机盐保存密码
    非对称/对称加密算法,hash算法
    自签名的应用--数字货币
    HTTPS原理
    Django中app的model相互引用问题
  • 原文地址:https://www.cnblogs.com/zhangchengbing/p/3238577.html
Copyright © 2011-2022 走看看