题目链接

题目

见链接。

题解

知识点:线性dp。

常规的状态 \(dp[i][j]\) 表示为到第 \(i\) 个岛上一步走了 \(j\) 能得到宝藏的最大值,会炸空间。注意到步数是就算从 \(1\) 开始走到结束,最多不会超过初始步数 \(d\) 的 \(\pm 250\) 。因此,改变状态为到第 \(i\) 个岛上一步的关于 \(d\) 的偏移量为 \(j+300\) (负数不能表示,所以整体加 \(300\))。

遍历 \(j\) 时要注意边界条件,如步数 \(d+j-300 \geq 1\) ,上一次的岛屿编号 \(i-(d+j-300) \geq 1\) 。

初始位置的宝藏要初始化进去,其他应该是负无穷。

答案不一定在最后一个岛上,因为可能跳不到,所以考虑在过程中取最大值即可。

时间复杂度 \(O(30000*600)\)

空间复杂度 \(O(30000*600)\)

代码

#include <bits/stdc++.h>

using namespace std;

int dp[30007][607];
int p[30007]; int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, d;
cin >> n >> d;
for (int i = 1, tmp;i <= n;i++) cin >> tmp, p[tmp]++;
memset(dp, -0x3f, sizeof(dp));
dp[d][300] = p[d];
int ans = p[d];
for (int i = d + 1;i <= 30000;i++) {
for (int j = 10;j <= 590;j++) {///偏移量不超过±250
if (1 <= d + j - 300 && i - (d + j - 300) >= 1) {///步数不小于1,岛屿不小于1
dp[i][j] = max({
dp[i - (d + j - 300)][j - 1],
dp[i - (d + j - 300)][j],
dp[i - (d + j - 300)][j + 1]
}) + p[i];
}
ans = max(dp[i][j], ans);///有可能跳不到30000而直接超过30000,但一定能经过最大值路径上的岛屿,每次保存最大值即可
///因为终点未知,也可以考虑逆推,因为起点固定
}
}
cout << ans << '\n';
return 0;
}

CF505C Mr. Kitayuta, the Treasure Hunter的更多相关文章

  1. codeforces 505C C. Mr. Kitayuta, the Treasure Hunter(dp)

    题目链接: C. Mr. Kitayuta, the Treasure Hunter time limit per test 1 second memory limit per test 256 me ...

  2. Codefores 506A Mr. Kitayuta, the Treasure Hunter( DP && dfs )

    A. Mr. Kitayuta, the Treasure Hunter time limit per test 1 second memory limit per test 256 megabyte ...

  3. [Codeforces 505C]Mr. Kitayuta, the Treasure Hunter

    Description The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. The is ...

  4. 【codeforces 505C】Mr.Kitayuta,the Treasure Hunter

    [题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...

  5. Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP

    题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...

  6. codeforces 505C Mr. Kitayuta, the Treasure Hunter(dp)

    题意:有30001个岛,在一条线上,从左到右编号一次为0到30000.某些岛屿上有些宝石.初始的时候有个人在岛屿0,他将跳到岛屿d,他跳跃的距离为d.如果当前他跳跃的距离为L,他下一次跳跃的距离只能为 ...

  7. [Codeforces Round#286] A.Mr. Kitayuta, the Treasure Hunter 【Normal DP..】

    题目链接:CF#286 - A 这场CF就这样爆零了...我真是太蒟蒻了... 题目分析 比赛的时候看到A题就发现不会,之后一直也没想出来,于是就弃了,还好不提交也不掉Rating... 比赛后看评论 ...

  8. 505C Mr. Kitayuta, the Treasure Hunter

    传送门 题目大意 一共有30000个位置,从第0个位置开始走,第一次走k步,对于每一次走步,可以走上一次的ki+1 ,ki ,ki-1步数(必须大于等于1),每个岛上有value,求最大能得到的val ...

  9. Codeforces 505C Mr. Kitayuta, the Treasure Hunter:dp【考虑可用范围】

    题目链接:http://codeforces.com/problemset/problem/505/C 题意: 有n个宝石,分别在位置p[i].(1 <= n,p[i] <= 30000) ...

  10. cf 506 A. Mr. Kitayuta, the Treasure Hunter

    不知道这个sb题怎么做错了.. /*#include <bits/stdc++.h> #define LL long long using namespace std; inline in ...

随机推荐

  1. 【MLA】一种内存泄漏分析方法

    项目地址:skullboyer/MLA (github.com) 介绍 MLA 即 Memory Leak Analyzer,是一个排查内存泄漏的分析器 实现机制是在malloc时记录分配位置信息,在 ...

  2. FileZilla 连接不上宝塔

    1,修改 pureftp的配置文件 ForcePassiveIP 为服务器的ip , 并去掉 # 2,FileZilla 使用明文连接

  3. Go-强制类型转换-T(x)

    类型转换 T(x) 具有相同的底层类型 数字类型之间可以互相转换(int系 uint系 float系),较大数转换成较小数会损失精度 字符串与切片之间的转换 string <==> []r ...

  4. [转帖]Navicat连接openGauss数据库报错

    news/2023/10/19 21:23:19 错误信息:fe_sendauth:invalid authentication request from server:AUTH_REQ_SASL_C ...

  5. [转帖]手摸手搭建简单的jmeter+influxdb+grafana性能监控平台

    我安装的机器是阿里云的centos8机器,其他的系统暂未验证 1.安装influxdb influxdb 下载地址https://portal.influxdata.com/downloads/,也可 ...

  6. [转帖]高性能 -Nginx 多进程高并发、低时延、高可靠机制在百万级缓存 (redis、memcache) 代理中间件中的应用

    https://xie.infoq.cn/article/2ee961483c66a146709e7e861 关于作者 前滴滴出行技术专家,现任 OPPO 文档数据库 mongodb 负责人,负责 o ...

  7. [转帖]并发控制- sched_yield 函数

    函数说明 函数原型 #include <sched.h> int sched_yield(void); 1 2 sched_yield的作用是让出处理器,调用时会导致当前线程放弃CPU,进 ...

  8. ESXi查看底层存储磁盘厂商型号的方式与方法

    ESXi查看底层存储磁盘厂商型号的方式与方法 背景 公司一台过保的服务器出现了磁盘告警 Vendor不太靠谱. 过保的机器就不管了 不买他们的服务器也不说一下是啥硬盘. 想自己替换,需要先获取磁盘的型 ...

  9. 使用Grafana 监控 SQLSERVER数据库

    使用Grafana 监控 SQLSERVER数据库 1.获取镜像信息以及启动镜像 docker pull awaragi/prometheus-mssql-exporter docker run -e ...

  10. Android APP升级时解析程序包时出现问题

    一个新的测试机在自动下载升级安装更新版本APP时,报出"解析程序包时出现问题"错误.原因众说纷纭, 一番搜索,下面的回答比较全面: https://stackoverflow.co ...