POJ 1836-Alignment(LIS-士兵列队)

来源:互联网 发布:mac怎么关闭开机声音 编辑:程序博客网 时间:2024/05/19 04:29
Alignment
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 16527 Accepted: 5424

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个士兵,给出他们的身高,可以让一些人出列,使得剩下的人排成的一列中:任何一个士兵都可以看到最前方或最后方。
计算最少的出列士兵人数。


解题思路:

DP,LIS。
满足题意的列队应该呈一个三角形,即从前往后递增或者从后往前递增。
所以分别从前往后求LIS、从后往前求LIS,然后找到一点使得前后递增之和最大(留在队列中的最大人数),所以需要出队的人数应该是总人数减去这个人数。

#include<iostream>#include<cstdio>#include<iomanip>#include<cmath>#include<cstdlib>#include<cstring>#include<map>#include<algorithm>#include<vector>#include<queue>using namespace std;#define INF 0x3f3f3f3f#define MAXN 1100double a[MAXN],dp1[MAXN],dp2[MAXN];int main(){#ifdef ONLINE_JUDGE#else    freopen("G:/cbx/read.txt","r",stdin);    //freopen("F:/cb/out.txt","w",stdout);#endif    ios::sync_with_stdio(false);    cin.tie(0);    int n;    cin>>n;    for(int i=0; i<n; ++i)        cin>>a[i];    //int x=0,y=0;    for(int i=0; i<n; ++i)//从前往后求LIS    {        dp1[i]=1;        for(int j=0; j<i; ++j)            if(a[j]<a[i])//要求严格递增                dp1[i]=max(dp1[i],dp1[j]+1);        //if(x<dp1[i]) x=dp1[i];    }    for(int i=n-1; i>=0; --i)//从后往前求LIS    {        dp2[i]=1;        for(int j=n-1; j>i; --j)            if(a[j]<a[i])                dp2[i]=max(dp2[i],dp2[j]+1);        //if(y<dp2[i]) y=dp2[i];    }    /*cout<<x<<" "<<y<<endl;    for(int i=0; i<n; i++)        cout<<dp1[i]<<" ";    cout<<endl;    for(int i=0; i<n; i++)        cout<<dp2[i]<<" ";    cout<<endl;*/    int ans=0;    for(int i=0; i<n; i++)        for(int j=i+1; j<n; j++)            if(dp1[i]+dp2[j]>ans)//找到一点使得递增之和最大                ans=dp1[i]+dp2[j];    cout<<n-ans<<endl;}


0 0
原创粉丝点击