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

Alignment
Time Limit: 1000MS   Memory Limit: 30000K
Total 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

8
1.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
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 1100
double 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>a[i];
    //int x=0,y=0;
    for(int i=0; i=0; --i)//从后往前求LIS
    {
        dp2[i]=1;
        for(int j=n-1; j>i; --j)
            if(a[j]ans)//找到一点使得递增之和最大
                ans=dp1[i]+dp2[j];
    cout<


你可能感兴趣的:(POJ,初级计划,ACM_DP)