zoukankan      html  css  js  c++  java
  • Alignment

    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.
     ***********************************************************************************************
    最长升序子序列
    ************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<cmath>
     7 using namespace std;
     8 int dp1[1010];
     9 int dp2[1010];
    10 double a[1010];
    11 double b[1010];
    12 int n,k;
    13 void find(double c[1010],int dp[1010])//找两次
    14   {
    15       int i,j,max2;
    16       memset(dp,0,sizeof(dp));
    17       dp[0]=1;
    18       for(i=1;i<n;i++)
    19       {
    20           max2=0;
    21           for(j=0;j<i;j++)
    22           {
    23            if(c[i]>c[j]&&dp[j]>max2)
    24              max2=dp[j];
    25           }
    26           dp[i]=max2+1;
    27       }
    28 
    29   }
    30   int main()
    31   {
    32       cin>>n;
    33       for(int i=0;i<n;i++)
    34        {
    35            scanf("%lf",&a[i]);
    36            b[n-i-1]=a[i];
    37        }
    38       find(a,dp1);
    39       find(b,dp2);
    40       int max1=0;
    41       for(int i=0;i<n;i++)
    42        for(int j=i+1;j<n;j++)
    43          {
    44              if(max1<dp2[i]+dp1[n-j-1])
    45                max1=dp2[i]+dp1[n-j-1];
    46          }
    47       cout<<n-max1<<endl;
    48       return 0;
    49   }
    View Code
  • 相关阅读:
    jedis操作redis事务练习
    jedis连接redis,测试操作redis常用的数据类型 String List Set Hash Zset
    多态的理解
    binarySeach方法
    数组重新认识
    String的 认识
    接口的 认识
    抽象类及抽象方法
    protected的深刻理解
    protected的认识
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3244546.html
Copyright © 2011-2022 走看看