HDU4512 - 吉哥系列故事——完美队形I(LCIS最长公共上升子序列的巧妙应用)

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4512

【题意】中文题

【分析】

一开始想枚举切分序列为两个序列,然后在找出最大的LCIS,时间要比后来看了大神的方法(只要在两个循环内直接分割序列)慢很多。下面写出两种方法的

 

【AC代码(我的枚举划分)】78ms

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 210
int a[MAXN], dp[MAXN][MAXN];

int main ()
{
    #ifdef SHY
        freopen("e:\\1.txt", "r", stdin);
    #endif
    int t;
    scanf ("%d%*c", &t);
    while(t--)
    {
        int n, ans = 0, buf, ei, ej;
        scanf ("%d%*c", &n);
        for (int i = 1; i <= n; i++)
            scanf ("%d%*c", &a[i]);
        for(int k = 1; k < n; k++)
        {
            buf = 0;//每个划分的LICS数
            for (int i = 1; i <= k; i++)
            {
                int maxn = 0;
                for (int j = n; j > k; j--)
                {
                    if (a[i] == a[j])
                    {
                        dp[i][j] = maxn+1;
                        if (buf < dp[i][j])
                            buf = dp[i][j], ei = i,ej = j;//ei,ej分别记录两个序列最后一个数,也就是序列末尾(最大的数,但是这个是对称的LCIS序列的最大值)
                    }
                    else
                    {
                        dp[i][j] = dp[i-1][j];
                        if(a[i] > a[j])
                            maxn = max(maxn,dp[i][j]);
                    }
                }
            }
            buf <<= 1;//因为是成对的,需要乘2
            //找出ei~ej之间是否还有更大的数(用二分可能还能稍微加快一点点,但是没必要,这点优化太小了),如果有需要算上这个数
            for (int i = ei+1; i < ej; i++)
            {
                if (a[i] > a[ei])
                {
                    buf++;
                    break;
                }
            }
            if (ans < buf)
                ans = buf;
        }
        printf ("%d\n", ans);
    }
    return 0;
}


 


 

 

【AC代码(参考别人后的)】15ms

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 210
int a[MAXN], dp[MAXN][MAXN];
//dp[i][j]表示的是a[1~i]和a[1~j]的LCIS,所以把第一重循环直接循环到n就不需要再外面多加一个循环来枚举切分大数组,他已经把所有切分都算进去了

int main ()
{
    #ifdef SHY
        freopen("e:\\1.txt", "r", stdin);
    #endif
    int t;
    scanf ("%d%*c", &t);
    while(t--)
    {
        int n, ans = 1;
        scanf ("%d%*c", &n);
        for (int i = 1; i <= n; i++)
            scanf ("%d%*c", &a[i]);
        for (int i = 1; i <= n; i++)
        {
            int maxn = 0;
            for (int j = n; j >= i; j--)//从后往前,因为要是前一半和后面一半倒序,把i也和上面重复算进去,是为了判断中间有没有更大的数字
            {
                if (a[i] == a[j])
                {
                    dp[i][j] = maxn+1;
                    //如果是本身只能算一个人
                    if (i == j)
                        ans = max(ans, dp[i][j]*2-1);
                    else
                        ans = max(ans, dp[i][j]*2);
                }
                else
                {
                    dp[i][j] = dp[i-1][j];
                    if (a[i] > a[j])
                        maxn = max(maxn,dp[i-1][j]);
                }
            }
        }
        printf ("%d\n", ans);
    }
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值