poj1836--Alignment(dp,最長上升子序列變形),poj1836
Alignment
| Time Limit: 1000MS |
|
Memory Limit: 30000K |
| Total Submissions: 13319 |
|
Accepted: 4282 |
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
81.86 1.86 1.30621 2 1.4 1 1.97 2.2
Sample Output
4
Source
Romania OI 2002
題目要求:給出n個人排成一排,踢出一些人,讓每個人都能看到最左端,或最後端,最小的踢出人數是?
計算出正序和倒序的最長上升子序列,然後統計:有兩種可能,一種是其中一個人是中間,那個人的身高最高,還有事兩個人的身高相同,這兩個人位置在中間,統計出最長的可能出現的隊伍長度,計算出最小的踢出人數
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int dp1[1200] , dp2[1200] ;double h[1200] ;int main(){ int i , j , n , min1 ; while(scanf("%d", &n)!=EOF) { min1 = 1200 ; h[0] = 0 ; h[n+1] = 0 ; for(i = 1 ; i <= n ; i++) scanf("%lf", &h[i]); memset(dp1,0,sizeof(dp1)); for(i = 1 ; i <= n ; i++) { for(j = 0 ; j < i ; j++) if( h[j] < h[i] && dp1[j]+1 > dp1[i] ) dp1[i] = dp1[j]+1 ; } memset(dp2,0,sizeof(dp2)); for(i = n ; i >= 1 ; i--) { for(j = n+1 ; j > i ; j--) if( h[j] < h[i] && dp2[j]+1 > dp2[i] ) dp2[i] = dp2[j]+1 ; } for(i = 1 ; i <= n ; i++) { for(j = i ; j <= n ; j++) { if(i == j) min1 = min(min1,n-(dp1[i]+dp2[j]-1) ); else min1 = min(min1, n-( dp1[i]+dp2[j] ) ); } } printf("%d\n", min1); }}
POJ2533 最長上升子序列問題
(1)背包問題. (poj1837,poj1276) (2)型如下表的簡單DP(可參考lrj的書 (poj3267,poj1836,poj1260,poj2533) 2.E[i,j]=opt{D[i-1,j] xi,
最長上升子序列
var
a,f{DP記錄},p{後面的}:array[1..1000]of longint;
i,j,k,n:longint;
begin
readln(n);
for i:=1 to n do
begin
read(a[i]);
f[i]:=1;{預先處理}
end;
for i:=n-1 downto 1 do
for j:=n downto i+1 do
if (a[i]<a[j])and(f[i]<=f[j])
then begin
p[i]:=j;
f[i]:=f[j]+1;
end;
k:=0;
for i:=n downto 1 do
if f[i]>j
then begin
j:=f[i];
k:=i;
end;
writeln(j);
while k<>0 do
begin
write(k,' ');
k:=p[k];
end;
end.
這是n2的演算法,大牛會寫nlog(n)的演算法,就是用堆來最佳化
這我聽過但忘了