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

 

posted @ 2013-08-07 21:31  persistent codeants  阅读(198)  评论(0编辑  收藏  举报