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
  • 相关阅读:
    [职场]最近聊到30岁以上的程序员,该何去何从了?你有啥想法?
    想跳槽涨薪,想进大厂,如何准备面试呢?
    [面试分享]想跳槽涨薪,想进大厂,如何准备面试呢?
    缓存穿透、缓存并发、缓存雪崩、缓存抖动、热点缓存、缓存双写一致性等问题...
    8天玩转并行开发——第八天 用VS性能向导解剖你的程序
    8天入门wpf—— 第一天 基础概念介绍
    8天玩转并行开发——第六天 异步编程模型
    8天入门wpf—— 第八天 最后的补充
    6天通吃树结构—— 第二天 平衡二叉树
    8天入门wpf—— 第七天 画刷
  • 原文地址:https://www.cnblogs.com/zhangchengbing/p/3238577.html
Copyright © 2011-2022 走看看