[Codeforces 505C]Mr. Kitayuta, the Treasure Hunter
Description
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.
Input
The 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.
Output
Print the maximum number of gems that Mr. Kitayuta can collect.
Sample Input
4 10
10
21
27
27
Sample Output
3
HINT
In the first sample, the optimal route is 0 → 10 (+1 gem) → 19 → 27 (+2 gems) → ...
题解
定义$f[i][j]$为走到$pos$为$i$时前一步步数为$j$的最大收益。
然而空间复杂度$O(n^2)$显然会爆...
后来稍微想想:因为步数每次只会变化$1$,所以我们考虑实际不同的步数远不足$N$,于是准备将步数值$hash$,$j$值就变成了取的$hash$值。
但后来又想:既然每次只会变化$1$,显然这步数是一段连续的区间,我们不需要$hash$这么麻烦,直接把数组整体左移就可以了。
我们假设不同的步数整体数值最小,那么是$1+2+3+…+250>30000$,那么其实我们数组第二维只要开$2*250=500$这么大就可以了。
//It is made by Awson on 2017.10.9
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define query QUERY
#define LL long long
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
using namespace std;
const int N = ; int n, d, a[N+], p;
int f[N+][], ans; void work() {
scanf("%d%d", &n, &d);
for (int i = ; i <= n; i++) {
scanf("%d", &p);
a[p]++;
}
memset(f, -, sizeof(f));
f[d][] = a[d];
for (int i = d; i <= N; i++)
for (int j = ; j <= ; j++)
if (f[i][j] != -) {
ans = Max(ans, f[i][j]);
int l = d+j-;
if (l > && i+l- <= N) f[i+l-][j-] = Max(f[i+l-][j-], f[i][j]+a[i+l-]);
if (i+l <= N) f[i+l][j] = Max(f[i+l][j], f[i][j]+a[i+l]);
if (i+l+ <= N) f[i+l+][j+] = Max(f[i+l+][j+], f[i][j]+a[i+l+]);
}
printf("%d\n", ans);
}
int main() {
work();
return ;
}
[Codeforces 505C]Mr. Kitayuta, the Treasure Hunter的更多相关文章
- Codeforces 505C Mr. Kitayuta, the Treasure Hunter:dp【考虑可用范围】
题目链接:http://codeforces.com/problemset/problem/505/C 题意: 有n个宝石,分别在位置p[i].(1 <= n,p[i] <= 30000) ...
- codeforces 505C Mr. Kitayuta, the Treasure Hunter(dp)
题意:有30001个岛,在一条线上,从左到右编号一次为0到30000.某些岛屿上有些宝石.初始的时候有个人在岛屿0,他将跳到岛屿d,他跳跃的距离为d.如果当前他跳跃的距离为L,他下一次跳跃的距离只能为 ...
- 505C Mr. Kitayuta, the Treasure Hunter
传送门 题目大意 一共有30000个位置,从第0个位置开始走,第一次走k步,对于每一次走步,可以走上一次的ki+1 ,ki ,ki-1步数(必须大于等于1),每个岛上有value,求最大能得到的val ...
- 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 505C】Mr.Kitayuta,the Treasure Hunter
[题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...
- Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP
题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...
- [Codeforces Round#286] A.Mr. Kitayuta, the Treasure Hunter 【Normal DP..】
题目链接:CF#286 - A 这场CF就这样爆零了...我真是太蒟蒻了... 题目分析 比赛的时候看到A题就发现不会,之后一直也没想出来,于是就弃了,还好不提交也不掉Rating... 比赛后看评论 ...
- cf 506 A. Mr. Kitayuta, the Treasure Hunter
不知道这个sb题怎么做错了.. /*#include <bits/stdc++.h> #define LL long long using namespace std; inline in ...
随机推荐
- x64系统安装ODAC问题经验分享
64bit系统安装ODAC经验分享 背景: 最近项目里面有用到 WCF+Entity Framework+oracle 这个架构用过的朋友应该都知道,Entity Framework要通过ODAC的方 ...
- Linux下I/O多路转接之epoll(绝对经典)
epoll 关于Linux下I/O多路转接之epoll函数,什么返回值,什么参数,我不想再多的解释,您不想移驾,我给你移来: http://blog.csdn.net/colder2008/artic ...
- WPF自学入门(十一)WPF MVVM模式Command命令
在WPF自学入门(十)WPF MVVM简单介绍中的示例似乎运行起来没有什么问题,也可以进行更新.但是这并不是我们使用MVVM的正确方式.正如上一篇文章中在开始说的,MVVM的目的是为了最大限度地降低了 ...
- 裸辞两个月,海投一个月,从Android转战Web前端的求职之路
前言 看到这个标题的童鞋,可能会产生两种想法: 想法一:这篇文章是标题党 想法二:Android开发越来越不景气了吗?前端越来越火了吗? 我一向不喜欢标题党,标题中的内容是我的亲身经历.我是2016年 ...
- 初学者如何查阅自然语言处理(NLP)领域学术资料
1. 国际学术组织.学术会议与学术论文 自然语言处理(natural language processing,NLP)在很大程度上与计算语言学(computational linguistics,CL ...
- Mego(06) - 关系数据库建模
框架中提供了多种数据注释以便可以全面的描述数据库结构特性. 自增列 可以使用注释声明指定列是数据库自增列,同时能指定自增的起始及步长. public class Blog { [Identity(, ...
- linux下的Shell编程(7)使用-x和-n调试shell程序
我们也可以在Shell下调试Shell Script脚本,当然最简单的方法就是用echo输出查看变量取值了.Bash也提供了真正的调试方法,就是执行脚本的时候用-x参数. sh -x filename ...
- 新概念英语(1-43)Hurry up!
新概念英语(1-43)Hurry up! How do you know Sam doesn't make the tea very often? A:Can you make the tea, Sa ...
- WebService(1-1)webservice调用
参考url : http://www.cnblogs.com/flying607/p/6254045.html 今天用动态创建客户端的方式调用webservice,报了这样一个错: 2017-01-0 ...
- CTF中常见密码题解密网站总结
0x00.综合 网站中包含大多编码的解码. http://web2hack.org/xssee/ https://www.sojson.com/ http://web.chacuo.net/ 0x01 ...