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

分析:要求最少出列数,就是留队士兵人数最大,

即左边的递增序列人数和右边的递减序列人数之和最大

因而可转化为求“最长升序子序列”和“最长降序子序列”问题

LIS之 n*n算法 (32MS)

#include<stdio.h>
#define max(a,b) a>b?a:b
int main()
{
    double a[1010];
    int dp1[1010],dp2[1010];
    int i,j,n,ans,max1,max2;
    while(scanf("%d",&n)!=EOF){
        for(i=1;i<=n;i++)
            scanf("%lf",&a[i]);
        ans=max1=max2=0;
        for(i=1;i<=n;i++){    //最长升序
            dp1[i]=1;
            for(j=1;j<=i;j++)
                if(a[j]<a[i]&&dp1[j]+1>dp1[i])
                    dp1[i]=dp1[j]+1;
        }
        for(i=n;i>=1;i--){      //最长降序
            dp2[i]=1;
            for(j=n;j>i;j--)
                if(a[j]<a[i]&&dp2[j]+1>dp2[i])
                    dp2[i]=dp2[j]+1;
        }
        for(i=1;i<=n;i++)       //最长升降序之和
            for(j=i+1;j<=n;j++)
                ans=max(ans,dp1[i]+dp2[j]);
        printf("%d\n",n-ans);  //最少出列数
    }
    return 0;
}<span style="font-family:KaiTi_GB2312;font-size:18px;">
</span>

LIS之 n*log n 算法 (235MS)

#include<stdio.h>
#define INF 3.0
double c[1010];
int bin_find1(int l,int r,double x)
{
    int mid=(l+r)/2;
    while(l!=r){
        if(x<c[mid])
            r=mid;
        else if(x>c[mid])
            l=mid+1;
        else
            return mid;
        mid=(l+r)/2;
    }
    return l;
}
int bin_find2(int l,int r,double x)
{
    int mid=(l+r)/2;
    while(l!=r){
        if(x>c[mid])
            r=mid;
        else if(x<c[mid])
            l=mid+1;
        else
            return mid;
        mid=(l+r)/2;
    }
    return l;
}
int main()
{
    double a[1010];
    int i,j,k,n,len_l,len_r,max;
    while(scanf("%d",&n)!=EOF){
        for(i=1;i<=n;i++)
            scanf("%lf",&a[i]);
        max=0;
        for(k=1;k<=n;k++){
            c[0]=-1;           //最长升序
            len_l=1;
            for(i=1;i<=k;i++){
                c[len_l]=INF;
                j=bin_find1(0,len_l,a[i]);
                c[j]=a[i];
                if(j==len_l)
                    len_l++;
            }
            len_l--;
            c[k]=INF;         //最长降序
            len_r=1;
            for(i=k+1;i<=n;i++){
                c[k+len_r]=-1;
                j=bin_find2(k,k+len_r,a[i]);
                c[j]=a[i];
                if(j==k+len_r)
                    len_r++;
            }
            len_r--;
            if(max<len_l+len_r)  //最长升降序的和
                max=len_l+len_r;
        }
        printf("%d\n",n-max);
    }
    return 0;
}
时间: 2024-10-30 11:41:03

poj 1836 Alignment(dp,LIS)的相关文章

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 1745 Divisibility(DP + 数学)

题目链接:http://poj.org/problem?id=1745 Description Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmetical expressions that evaluate to different values. Let us, f

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 2533 Longest Ordered Subsequence(dp LIS)

Language: Default Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 33986   Accepted: 14892 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric seq

codeforces 340D Bubble Sort Graph(dp,LIS)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud  Bubble Sort Graph Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple al

HDU 1087 Super Jumping! Jumping! Jumping! (DP+LIS)

题意:给定一个长度为n的序列,让你求一个和最大递增序列. 析:一看,是不是很像LIS啊,这基本就是一样的,只不过改一下而已,d(i)表示前i个数中,最大的和并且是递增的, 如果 d(j) + a[i] > d(i)  d(i) = d(j) + a[i] (这是保证和最大),那么怎么是递增呢?很简单嘛,和LIS一样 再加上a[i] > a[j],这不就OK了. 代码如下: #include <cstdio> #include <iostream> #include &l

POJ 1390 Blocks(DP + 思维)题解

题意:有一排颜色的球,每次选择一个球消去,那么这个球所在的同颜色的整段都消去(和消消乐同理),若消去k个,那么得分k*k,问你消完所有球最大得分 思路:显然这里我们直接用二位数组设区间DP行不通,我们不能表示出“合并”这种情况.我们先把所有小块整理成连续的大块. 我们用click(l,r,len)表示消去l到r的所有大块和r后len块和r颜色一样的小块的最大得分.那么这样我们可以知道,click(l,r,len)只有两种情况: 1.r直接和后面len全都消去 2.r带着len先和前面的一样的颜色

HDU 5078 Revenge of LIS II(dp LIS)

Problem Description In computer science, the longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as po

hdu----(1257)最少拦截系统(dp/LIS)

最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19172    Accepted Submission(s): 7600 Problem Description 某 国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能 超过前一发的