POJ 1836 Alignment(LIS和LDS的结合题)

题目地址:点击打开链接

题意:有一队排列好的士兵,长官让一些人除队后,每个人都可以看到左边或者右边的无穷远处


思路:题没怎么看懂,但觉得大致意思就是求最长上升子序列,这样就能看到他左边和右边的人了,看了大神的博客才知道是求LIS和LDS(最长下降子序列,估计应该叫这个名字吧)的组合情况,但还是写错了一次,看下面错误代码,

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

typedef long long ll;
using namespace std;

const int maxn = 1010;

double a[maxn];
int dp1[maxn],dp2[maxn];

int main()
{
    int n,i,j;
    while(scanf("%d",&n) != EOF)
    {
        for(i=1; i<=n; i++)
        {
            scanf("%lf",&a[i]);
        }
        memset(dp1,0,sizeof(dp1));
        memset(dp2,0,sizeof(dp2));
        for(i=1; i<=n; i++)
        {
            int max1 = 0;//这里是int类型
            for(j=1; j<i; j++)
            {
                if(a[i] > a[j] && dp1[j] > max1)
                {
                    max1 = dp1[j];
                }
            }
            dp1[i] = max1 + 1;
        }
        for(i=n; i>=1; i--)
        {
            int max2 = 0;
            for(j=n; j>=i+1; j--)
            {
                if(a[i] > a[j] && dp2[j] > max2)
                {
                    max2 = dp2[j];
                }
            }
            dp2[i] = max2 + 1;
        }
        int max3 = 0;
        for(i=1; i<=n; i++)
        {
            for(j=i+1; j<=n; j++)
            {
                if(dp1[i] + dp2[j] > max3)
                {
                    max3 = dp1[i] + dp2[j];
                }
            }
        }
        printf("%d\n",n-max3);
    }
    return 0;
}

正着求一次最长上升子序列,倒着求一次最长上升子序列,怕超时可以用O(nlogn)算法

错误代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

typedef long long ll;
using namespace std;

const int maxn = 1010;

double a[maxn];
int dp1[maxn],dp2[maxn];

int main()
{
    int n,i,j;
    while(scanf("%d",&n) != EOF)
    {
        for(i=1; i<=n; i++)
        {
            scanf("%lf",&a[i]);
        }
        memset(dp1,0,sizeof(dp1));
        memset(dp2,0,sizeof(dp2));
        for(i=1; i<=n; i++)
        {
            int max1 = 0;//这里是int类型
            for(j=1; j<i; j++)
            {
                if(a[i] > a[j] && dp1[j] > max1)
                {
                    max1 = dp1[j];
                }
            }
            dp1[i] = max1 + 1;
        }
        for(i=1; i<=n; i++)
        {
            int max2 = 0;
            for(j=1; j<i; j++)
            {
                if(a[i] < a[j] && dp2[j] > max2)
                {
                    max2 = dp2[j];
                }
            }
            dp2[i] = max2 + 1;
        }
        int max3 = 0;
        for(i=1; i<=n; i++)
        {
            for(j=i+1; j<=n; j++)
            {
                if(dp1[i] + dp2[j] > max3)
                {
                    max3 = dp1[i] + dp2[j];
                }
            }
        }
        printf("%d\n",n-max3);
    }
    return 0;
}

正着求了一次最长上升子序列,又倒着求了一次最长下降子序列,结果测试数据就过不了,太逗了,这种低级错误都犯

大神地址:http://cavenkaka.iteye.com/blog/1542421

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值