题目链接:

C. Mr. Kitayuta, the Treasure Hunter

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard 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.

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.

Examples
input
4 10
10
21
27
27
output
3
input
8 8
9
19
28
36
45
55
66
78
output
6
input
13 7
8
8
9
16
17
17
18
21
23
24
24
26
30
output
4

题意:

有30000个点,n个点放在pi上,第一次去d,每次走的距离只能为上次行走的距离l,l+1,l-1,但是一定要前进;问最后能得到的最大值是多少;

思路:

可以发现行走长度的变化范围不超过250;dp[i][j]表示走j长到达i能得到的最大值;转移方程看代码吧;

AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=3e4+;
const int N=3e4;
const int inf=;
int n,d;
int p[maxn],num[maxn],dp[maxn][];
int main()
{
scanf("%d%d",&n,&d);
for(int i=;i<=n;i++)
{
scanf("%d",&p[i]);
num[p[i]]++;
}
memset(dp,-inf,sizeof(dp));
dp[d][]=num[d];
for(int i=;i<=N;i++)
{
for(int j=;j<=;j++)
{
int x=j-+d;
if(i+x>i&&i+x<=N)dp[i+x][j]=max(dp[i+x][j],dp[i][j]+num[i+x]);
if(i+x+>i&&i+x+<=N)dp[i+x+][j+]=max(dp[i+x+][j+],dp[i][j]+num[i+x+]);
if(i+x->i&&i+x-<=N)dp[i+x-][j-]=max(dp[i+x-][j-],dp[i][j]+num[i+x-]);
}
}
int ans=;
for(int i=;i<=N;i++)
{
for(int j=;j<=;j++)
{
ans=max(ans,dp[i][j]);
}
}
cout<<ans<<"\n"; }

codeforces 505C C. Mr. Kitayuta, the Treasure Hunter(dp)的更多相关文章

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

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

  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(dp)

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

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

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

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

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

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

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

  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. cf 506 A. Mr. Kitayuta, the Treasure Hunter

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

随机推荐

  1. jQuery on() 和 live

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...

  2. Day 4 Linux基础

    Linux基础(指令篇) 一.Linux命令 1.Linux命令行的语法格式: 命令+选项+参数 命令:告诉Linux(UNIX)操作系统做(执行)什么. 选项:说明命令运行的方式(可以改变命令的功能 ...

  3. qu de hanzi 首字母

    Function hztopy(hzpy As String) As StringDim hzstring As String, pystring As StringDim hzpysum As In ...

  4. itext A4纸张横向创建PDF

    import java.awt.Color;import java.io.FileOutputStream;import java.io.IOException; import com.lowagie ...

  5. CentOS 7.5 初始网络配置

    最近刚装完 CentOS 7.5 系统,由于网络不通,导致无法用 yum 命令下载软件,经过了各种折腾,终于搞定了,这里讲解一下 如何设置初始网络. 本案例环境  VmWare 11.0 , 操作系统 ...

  6. [转] SQL Server中变量的声明和使用方法

    原文地址 SQL Server中变量的声明和使用方法 声明局部变量语法: DECLARE @variable_name DataType 其中 variable_name为局部变量的名称,DataTy ...

  7. Mysql 性能优化20个原则(3)

    12. Prepared Statements Prepared Statements很像存储过程,是一种运行在后台的SQL语句集合,我们可以从使用 prepared statements 获得很多好 ...

  8. [Javascript] Wrap fireEvent with fireEventAsync

    The idea is wrap a object with all its function methods and add some additional handling into a new ...

  9. linux下查看网卡信息的命令

    rhel 内核版本号信息: [root@hvrhub ~]# uname -a Linux hvrhub 2.6.18-308.el5 #1 SMP Fri Jan 27 17:17:51 EST 2 ...

  10. react 创建组件 (二)component

    因为ES6对类和继承有语法级别的支持,所以用ES6创建组件的方式更加优雅,下面是示例: import React from 'react'; class Greeting extends React. ...