POJ 1836 Alignment( LIS )

来源:互联网 发布:任我游软件 编辑:程序博客网 时间:2024/05/22 10:26
Alignment
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 13581 Accepted: 4369

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
给你n个数,判断最少去掉多少个数,从中间往左是递减的序列,往右是递增的序列
需注意的是中间可能为两个相同的值,如 1 2 3 3 2 1     输出为0
刚看到这个这个题的时候,感觉跟uva 10534那个题很相似,只不过这个存在偶数的情况,在做的时候想当然以为左右对称了,结果写复杂了,改的也比较乱,悲剧的WA了一下午。。。。
正序求一次LIS,得到各个点的最大递增序列的长度(存在数组b中),然后再同样逆序求一次(存在数组b1中),这样我们可以得到以每一个点为中间点,往左递减,往右递增的序列的长度(存在数组s中,,s[i] = b[i] + b1[i]-1 , 减去1是因为求了两次LIS,中间点加了两遍),这样得到的还不是最终的结果,还要考虑中间是两个相同的值的情况,这时可以从两头往中间找,找到相同的两个数,他们对应的s[i]相应加1。。
数组s中的最大值就是站队所需要的最大人数,最后结果为n-max(s)
#include <iostream>#include <stdlib.h>#include <cstdio>using namespace std;const int N = 1010;const int inf = 99999999;int erfen(double key,double c[],int right){    int left=0;    int mid=(left + right) / 2;    while(left <= right)    {        if(c[mid] < key)            left=mid+1;        else if(c[mid] > key)            right=mid-1;        else return mid;        mid=(left + right) / 2;    }    return left;}int main(){    int n;    while(cin>>n)    {        double a[N],c[N];        int b[N];        double a1[N];        for(int i=0; i<n; i++)        {            scanf("%lf",&a[i]);            a1[n-i-1]=a[i];        }        for(int i=0; i<=n; i++)            c[i]=inf;        c[0]=-1;        c[1]=a[0];        b[0]=1;        for(int i=1; i<n; i++)        //正序求LIS        {            int j=erfen(a[i],c,n);            c[j]=a[i];            b[i]=j;        }        int b1[N];        double c1[N];        for(int i=0; i<=n; i++)            c1[i]=inf;        c1[0]=-1;        c1[1]=a1[0];        b1[0]=1;        for(int i=1; i<n; i++)          //逆序求LIS        {            int j=erfen(a1[i],c1,n);            c1[j]=a1[i];            b1[i]=j;        }        int s[N];        for(int i=0; i<n; i++)            s[i]=b[i]+b1[n-i-1]-1;       //以每个点为中间点的队列长度        for(int i=0; i<n; i++)        {            if(i==n-i-1)                break;            if(a[i]==a1[i])              //判断中间点为两个相同值的情况,如1 2 3 3 2 1            {                int x=b[i]+b1[i];                if(x > s[i])                    s[i]=x;            }        }        int ans=0;        for(int i=0; i<n; i++)            if(ans<s[i])                ans=s[i];        printf("%d\n",n-ans);    }    return 0;}


                                             
0 0
原创粉丝点击