AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

AcWing 3073. 雷涛的小猫    原题链接    中等

作者: 作者的头像   炽热的 ,  2022-05-29 15:44:56 ,  所有人可见 ,  阅读 166


4


动态规划

$f(i, j)$ 表示小猫正在第 $i$ 棵树上的第 $j$ 层时 获得柿子的最大值

状态转移:小猫可以从 树的上一层下来 或者 其他树上跳下来

  • 因此 $f(i, j) = max(f(i, j + 1), f(k, j + d)) + w(i, j) $, $1 <= k <= n$
朴素
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 2010;

int n, h, d;
int w[N][N];
int f[N][N];

int main()
{
    cin >> n >> h >> d;

    for (int i = 1; i <= n; i ++ ) 
    {
        int cnt;
        cin >> cnt;
        while (cnt -- )
        {
            int j;
            cin >> j;
            w[i][j] ++ ;
        }
    }

    for (int j = h; ~j; j -- )
        for (int i = 1; i <= n; i ++ )
        {
            f[i][j] = f[i][j + 1] + w[i][j];
            for (int k = 1; k <= n; k ++ )
                f[i][j] = max(f[i][j], f[k][j + d] + w[i][j]);
        }

    int res = 0;
    for (int i = 1; i <= n; i ++ ) res = max(res, f[i][0]);

    cout << res;

    return 0;
}
上面 $ n^{3} $ 做法必然超时,然后发现第三层循环是找上面层的最大值, 用一个数组记录每层的最大值就可以了

AC 代码

#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 4010;

int n, h, d;
int w[N][N];
int f[N][N], g[N];

int main()
{
    scanf("%d%d%d", &n, &h, &d);

    for (int i = 1; i <= n; i ++ ) 
    {
        int cnt;
        scanf("%d", &cnt);
        while (cnt -- )
        {
            int j;
            scanf("%d", &j);
            w[i][j] ++ ;
        }
    }

    for (int j = h; ~j; j -- )
        for (int i = 1; i <= n; i ++ )
        {
            f[i][j] = max(f[i][j + 1], g[j + d]) + w[i][j];
            g[j] = max(g[j], f[i][j]);
        }

    int res = 0;
    for (int i = 1; i <= n; i ++ ) res = max(res, f[i][0]);

    printf("%d\n", res);

    return 0;
}

3 评论


用户头像
kimmmm   2022-05-29 23:41         踩      回复

mobai

用户头像
炽热的   2022-05-30 00:02         踩      回复

niyaoxiewan

用户头像
kimmmm   2022-06-01 16:57    回复了 炽热的 的评论         踩      回复

haode


你确定删除吗?

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息