POJ 1836 Alignment 枚举中间点双向求LIS

点击打开链接

Alignment

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 13590   Accepted: 4375

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

一排士兵按照标号站成一排,让一些士兵出列,使得剩下的士兵朝左或者朝右看都能看到无穷远处。

where each soldier can see by looking lengthwise the line at least one of the line‘s extremity (left or right).士兵能够朝着至少一个方向看到无穷远处。

新队列的身高要满足这个式子:tail[1]<tail[2]<.....tail[i]  tail[i+1]>tail[i+2]>......tail[n]

把从1枚举到n-1,分别求出1到i的最长上升子序列和i+1到n的最长下降子序列,最后用n减去他们的最大和。

//172K	297MS
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
double tail[1007],dp[1007],s[1007];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int maxx=-1;
        for(int i=1;i<=n;i++)
            scanf("%lf",&tail[i]);

        memset(dp,0,sizeof(dp));
        for(int i=1;i<n;i++)//枚举中间点
        {
            //求1到i的最长上升子序列
            dp[1]=tail[1];
            int low,high,mid,len=1;
            for(int j=2;j<=i;j++)
            {
                low=1;high=len;
                while(low<=high)
                {
                    mid=(low+high)>>1;
                    if(dp[mid]<tail[j])low=mid+1;
                    else high=mid-1;
                }
                dp[low]=tail[j];
                if(low>len)len++;
            }

            //求i+1到n的最长下降子序列
            memset(dp,0,sizeof(dp));
            memset(s,0,sizeof(s));
            int k=0,len2=1;
            for(int j=n;j>i;j--)//倒序一下
                s[++k]=tail[j];
            dp[1]=s[1];
            for(int j=2;j<=k;j++)//倒着求最长上升子序列
            {
                low=1;high=len2;
                while(low<=high)
                {
                    mid=(low+high)>>1;
                    if(dp[mid]<s[j])low=mid+1;
                    else high=mid-1;
                }
                dp[low]=s[j];
                if(low>len2)len2++;
            }
            if(len+len2>maxx)maxx=len+len2;
        }
        printf("%d\n",n-maxx);
    }
    return 0;
}

时间: 2024-11-14 18:26:11

POJ 1836 Alignment 枚举中间点双向求LIS的相关文章

poj 1836 Alignment 排队

poj   1836   Alignment http://poj.org/problem?id=1836 题意:有士兵n个,根据编号排为一列,但是身高不一,现在要求去掉几个人,使得剩下的每一个人可以向左或向右看到队头.问:至少去掉的士兵数. dp动态规划 之 双向LIS问题 1 /* 2 Problem: 1836 User: bibier 3 Memory: 712K Time: 63MS 4 Language: G++ Result: Accepted 5 */ 6 //动态规划 之 双向

SHUOJ A序列 &amp;&amp; POJ 1836 Alignment [动态规划 LIS]

A序列 发布时间: 2017年7月8日 21:16   最后更新: 2017年7月8日 22:29   时间限制: 1000ms   内存限制: 128M 描述 如果一个序列有奇数个正整数组成,不妨令此序列为a1,a2,a3,...,a2?k+1 (0<=k ),并且a1,a2...ak+1 是一个严格递增的序列,ak+1,ak+2,...,a2?k+1 ,是一个严格递减的序列,则称此序列是A序列. 比如1 2 5 4 3就是一个A序列. 现在Jazz有一个长度为n 的数组,他希望让你求出这个数

POJ 1836 Alignment

http://poj.org/problem?id=1836 题意:给出一排士兵的身高,求出至少需要移除多少个士兵可以使得剩下的士兵往左看或者是往右看可以看到无穷远处. 思路:士兵的分布最终要呈三角形分布,我们从左边和右边分别求一个最长递增子序列,然后最后只需要一一枚举就可以了. 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 const int maxn = 1000 + 5; 6 7 i

poj 1836 Alignment(dp,LIS)

链接:poj 1836 题意:士兵站成一行,求最少要多少的士兵出列, 使得每个士兵都能至少看到一个最边上的士兵 中间某个人能看到最边上的士兵的条件是: 该士兵的身高一定强大于他某一边(左边或右边)所有人的身高, 身高序列可以是: 1  2  3   4   5   4   3   2   1  或者 1   2   3   4   5   5   4   3   2   1 分析:要求最少出列数,就是留队士兵人数最大, 即左边的递增序列人数和右边的递减序列人数之和最大 因而可转化为求"最长升序子

poj 1836 Alignment(线性dp)

题目链接:http://poj.org/problem?id=1836 思路分析:假设数组为A[0, 1, …, n],求在数组中最少去掉几个数字,构成的新数组B[0, 1, …, m]满足条件B[0] < B[1] <….<B[i] 且 B[i+1] > … > B[m]; 该问题实质为求A[0, …, k]的最长递增子序列和A[j, …, n]中的最长递减子序列(0 <= k <= n, 0 <= j <= n, k < j);所以求出A[0

poj 1836 Alignment (DP LIS)

Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13839   Accepted: 4478 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 cap

POJ 1836 Alignment(DP max(最长上升子序列 + 最长下降子序列))

Alignment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14486   Accepted: 4695 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 cap

[poj 1836] Alignment dp

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

POJ 1836 Alignment 最长递增子序列(LIS)的变形

大致题意:给出一队士兵的身高,一开始不是按身高排序的.要求最少的人出列,使原序列的士兵的身高先递增后递减. 求递增和递减不难想到递增子序列,要求最少的人出列,也就是原队列的人要最多. 1 2 3 4 5 4 3 2 1 这个序列从左至右看前半部分是递增,从右至左看前半部分也是递增.所以我们先把从左只右和从右至左的LIS分别求出来. 如果结果是这样的: A[i]={1.86 1.86 1.30621 2 1.4 1 1.97 2.2} //原队列 a[i]={1 1 1 2 2 1 3 4} b[