| dp-the Treasure Hunter
题目:
A. Mr. Kitayuta, the Treasure Huntertime limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. The islands are evenly spaced along a line, numbered from 0 to 30000 from the west to the east. These islands are known to contain many treasures. There are n gems in the Shuseki Islands in total, and the i-th gem is located on island pi.
Mr. Kitayuta has just arrived at island 0. With his great jumping ability, he will repeatedly perform jumps between islands to the east according to the following process:
- First, he will jump from island 0 to island d.
- After that, he will continue jumping according to the following rule. Let l be the length of the previous jump, that is, if his previous jump was from island prev to island cur, let l = cur - prev. He will perform a jump of length l - 1, l or l + 1 to the east. That is, he will jump to island (cur + l - 1), (cur + l) or (cur + l + 1) (if they exist). The length of a jump must be positive, that is, he cannot perform a jump of length 0 when l = 1. If there is no valid destination, he will stop jumping.
Mr. Kitayuta will collect the gems on the islands visited during the process. Find the maximum number of gems that he can collect.
InputThe first line of the input contains two space-separated integers n and d (1 ≤ n, d ≤ 30000), denoting the number of the gems in the Shuseki Islands and the length of the Mr. Kitayuta's first jump, respectively.
The next n lines describe the location of the gems. The i-th of them (1 ≤ i ≤ n) contains a integer pi (d ≤ p1 ≤ p2 ≤ ... ≤ pn ≤ 30000), denoting the number of the island that contains the i-th gem.
OutputPrint the maximum number of gems that Mr. Kitayuta can collect.
ExamplesInputCopy4 10
10
21
27
27OutputCopy3InputCopy8 8
9
19
28
36
45
55
66
78OutputCopy6InputCopy13 7
8
8
9
16
17
17
18
21
23
24
24
26
30OutputCopy4NoteIn the first sample, the optimal route is 0 → 10 (+1 gem) → 19 → 27 (+2 gems) → ...
In the second sample, the optimal route is 0 → 8 → 15 → 21 → 28 (+1 gem) → 36 (+1 gem) → 45 (+1 gem) → 55 (+1 gem) → 66 (+1 gem) → 78 (+1 gem) → ...
In the third sample, the optimal route is 0 → 7 → 13 → 18 (+1 gem) → 24 (+2 gems) → 30 (+1 gem) → ...
思路和实现都不难的动态规划
(._. )
做的时候没看出来长度的限制 担心复杂度太大所以不敢写
(._. )
菜鸡
(._. )
dp[i][j]表示到编号为 i 的岛且上次跳跃长度为 j 时能取到的最多的gems的数目。
注意:岛的编号限制在30000以内,且每次最多增长一步,第一步跳跃长度为d,总的跳跃长度 = d + (d + 1) + (d + 2) + ... + (d + 245) ≥ 1 + 2 + ... + 245 = 245·(245 + 1) / 2 = 30135 > 30000。所以跳跃长度最长为(d+245),最短为(d-245),因此 j 的枚举长度在[d-245,d+245]之间,第二维的空间缩小到500。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<set> using namespace std; const int maxn = *1e4+; int dp[maxn][];;
int num[maxn]; int main()
{
int base;
int a, b;
int n, m, d;
int i, j, k;
scanf("%d %d",&n,&d);
for(i = ; i <= n; i++)
scanf("%d", &a), num[a]++;
base = max(d -,);
memset(dp, -, sizeof dp);
dp[d][d-base] = num[d];
int ans=;
for(i = d ; i <= ; i++)
{
for( j = ;j <= ; j++)
{
if(dp[i][j] == -) continue;
for(k = -; k < ; k++)
{
if( j + k < || base + i + j + k > ) continue;
dp[i + base + j + k][j + k] = max(dp[base + i + j + k][j + k], dp[i][j] + num[i + j + k + base]);
}
ans = max(ans, dp[i][j]);
}
}
cout << ans << endl;
return ;
}
| dp-the Treasure Hunter的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP
题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...
- codeforces 505C Mr. Kitayuta, the Treasure Hunter(dp)
题意:有30001个岛,在一条线上,从左到右编号一次为0到30000.某些岛屿上有些宝石.初始的时候有个人在岛屿0,他将跳到岛屿d,他跳跃的距离为d.如果当前他跳跃的距离为L,他下一次跳跃的距离只能为 ...
- [Codeforces Round#286] A.Mr. Kitayuta, the Treasure Hunter 【Normal DP..】
题目链接:CF#286 - A 这场CF就这样爆零了...我真是太蒟蒻了... 题目分析 比赛的时候看到A题就发现不会,之后一直也没想出来,于是就弃了,还好不提交也不掉Rating... 比赛后看评论 ...
- Codeforces 505C Mr. Kitayuta, the Treasure Hunter:dp【考虑可用范围】
题目链接:http://codeforces.com/problemset/problem/505/C 题意: 有n个宝石,分别在位置p[i].(1 <= n,p[i] <= 30000) ...
- 【树形dp】Treasure Hunt I
[ZOJ3626]Treasure Hunt I Time Limit: 2 Seconds Memory Limit: 65536 KB Akiba is a dangerous coun ...
- 【codeforces 505C】Mr.Kitayuta,the Treasure Hunter
[题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...
- 505C Mr. Kitayuta, the Treasure Hunter
传送门 题目大意 一共有30000个位置,从第0个位置开始走,第一次走k步,对于每一次走步,可以走上一次的ki+1 ,ki ,ki-1步数(必须大于等于1),每个岛上有value,求最大能得到的val ...
随机推荐
- MVC控制器返回一个list 视图接收
控制器 public ActionResult InfoFrame() { List<Users> list = new List<Users>(); if (Session[ ...
- H5取经之路——CSS基础语法
一.CSS常用选择器 [选择器的命名规则] * 1.只能有字母数字下划线组成,不能有其他任何字符 * 2.开头不能是数字 [通用选择器] * 1.写法:*{} * 2.选中页面中的所有标签 * 3.优 ...
- 快速安装Java环境
1.部署jdk8 jdk包地址:https://pan.baidu.com/s/1QNGpapGuex00F6HQ5pynHgtar -xzf jdk-8u60-linux-x64.tar.gz #安 ...
- 408 JavaScript 变量、数据类型、正则
JavaScript 特点 是一门解释性脚本语言 .基于对象脚本编程.简单性(弱类型).安全性.动态性.跨平台 作用: 初学js 引入方式 与html有相同之处 也是3种1 用JavaScript前缀 ...
- jsp注释<%-- --%>和<!-- -->的区别
最近在写JSP页面注释的时候,遇到一个问题,在JSP页面引用的静态属性资源文件时,在浏览器控制台报错,当我把引用的标签注释掉后,用的是<!-- -->.然后浏览器仍然报了之前那个错,经过查 ...
- AWK如何打印从某一列到最后一列的内容
awk -F " " '{for (i=4;i<=NF;i++)printf("%s ", $i);print ""}' file
- @Autowired mapper 层次 bean 带红线
在利用@Autowired 注解创建bean 时候 有时间会带有下滑红色横线 给人一种报错的感觉 下面是去除红线的办法 将颜色红色error 等级降低为黄色warn 即可
- iOS -- Effective Objective-C 阅读笔记 (9)
// 将类的实现方法代码反三到便于管理的数个分类之中. // 类中经常容易填满各种方法, 而这些方法的代码则全部堆在一个巨大的实现文件中, 有时这么做事不合理的,因为即使通过重构把这个类 ...
- Monkey自动化脚本(一)
1.Monkey简介 Monkey-猴子,通过Monkey程序模拟用户触摸屏幕.滑动Trackball. 按键等操作来对设备上的程序进行压力测试,检测程序多久的时间会发生异常,主要用于Android ...
- 前端Vue 源码分析-逻辑层
Vue 源码分析-逻辑层 预期的效果: 监听input的输入,input在输入的时候,会触发 watch与computed函数,并且会更新原始的input的数值.所以直接跟input相关的处理就有3处 ...