poj1836 Alignment

Alignment
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 11707   Accepted: 3730

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

[Submit]   [Go Back]   [Status]   [Discuss]


最近在進行動態規劃的專題練習。。。

題意:令到原隊列的最少士兵出列後,使得新隊列任意一個士兵都能看到左邊或者右邊的無窮遠處。

可以是遞增序列。也可以是遞減序列。更可以是如下圖:


 

所以這是雙向lis問題,剛開始的時候還沒想到是lis問題,就按照自己的方法做。結果wa。後來發現有很多bug。。

看了discuss纔想到是lis問題。還是練得少呀!!


#include<cstdio>
#include<cstring>
int dp1[1006];   //dp1[i]代表以i結尾的最長上升子序列
int dp2[1006];   //dp2[i]代表以i開始的最長下降子序列
int maxdp[1006];  //maxdp[i]代表以0~i中dp1中的最最大值
double p[1006];  
int Max(int a,int b)
{
    return a>b?a:b;
}    
int main()
{
    int n,i,j,mm;
    while(scanf("%d",&n)!=EOF)
    {
		mm = 0;
        for(i=1;i<=n;i++)
			scanf("%lf",&p[i]);
        maxdp[1] = dp1[1] = 1;
		maxdp[0] = 0;
        for(i=2;i<=n;i++)    //兩個lis算法
		{
			dp1[i] = 1;
			for(j=1;j<i;j++)
			{
				if(p[j]<p[i])
					dp1[i] = Max(dp1[i],dp1[j]+1);
				
			} 
			maxdp[i] = Max(maxdp[i-1],dp1[i]);
		}
		dp2[n] = 1;
		for(i=n-1;i>=1;i--)
		{
			dp2[i] = 1;
			for(j=n;j>i;j--)
			{
				if(p[j]<p[i])
					dp2[i] = Max(dp2[i],dp2[j]+1);
				
			} 
			mm = Max(mm,maxdp[i]+dp2[i+1]);  //把以i前面的上升序列最大值和以i+1結尾的最長下降子序
											//列長度加起來和前面對比,這樣就是把全部的情況都算了一遍
		}

        printf("%d\n",n-mm);    
    }    
    return 0;
}    



發佈了58 篇原創文章 · 獲贊 89 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章