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

这个题一开始就想到DP了 。dp[i][j]表示跳到了下标 i 上次跳了 j 个长度的最大得分。

但是d的范围有点大 , 我试了一下开二维记忆化搜索死机了。

后来大洲爷说了一下思路 , d <= 2000  dp , 否则 dfs , d > 2000的话层数应该10来层就OK了吧 。

其几天考试没时间写,刚考完才有时间补一下题来着。 一写就过了~

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
using namespace std;
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lr rt<<1
#define rr rt<<1|1
typedef long long LL;
const int oo = 1e9+;
const double PI = acos(-1.0);
const double eps = 1e- ;
const int N = ;
const int mod = ;
int n , d , x , dp[N][] , cnt[N]; int dfs( int i , int l ) {
if( i > x ) return ;
int tmp = cnt[i];
if( l > ) tmp = max( tmp , cnt[i] + dfs( i + l - , l - ) );
tmp = max( tmp , cnt[i] + dfs( i + l , l ) );
tmp = max( tmp , cnt[i] + dfs( i + l + , l + ) );
return tmp ;
} int DP( int i , int l ) {
if( i > x ) return ;
if( dp[i][l] == - ) {
int &tmp = dp[i][l] ; tmp = cnt[i];
if( l > ) tmp = max( tmp , cnt[i] + DP( i + l - , l - ) );
tmp = max( tmp , cnt[i] + DP( i + l , l ) );
tmp = max( tmp , cnt[i] + DP( i + l + , l + ) );
}
return dp[i][l];
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif // LOCAL
ios::sync_with_stdio(false);
while( cin >> n >> d ) {
memset( cnt , , sizeof cnt ) ;
memset( dp , - , sizeof dp ) ;
for( int i = ; i < n ; ++i ) {
cin >> x ; cnt[x]++;
}
if( d > ) cout << dfs( d , d ) << endl ;
else cout << DP(d,d) << endl ;
}
}

Codefores 506A Mr. Kitayuta, the Treasure Hunter( DP && dfs )的更多相关文章

  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. Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP

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

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

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

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

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

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

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

  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 505C】Mr.Kitayuta,the Treasure Hunter

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

  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. Crash的数字表格(莫比乌斯反演)

    Crash的数字表格 Description 今天的数学课上,Crash小朋友学习了最小公倍数(Least Common Multiple).对于两个正整数a和b,LCM(a, b)表示能同时被a和b ...

  2. 【知识强化】第三章 存储系统 3.4 主存储器与CPU的连接

    我们这节课来看一下关于主存的一些知识.我们将要讲解主存的简单的模型和主存与CPU连接的连接原理. 我们之前呢在第一章已经学过了存储器的构成,包括了存储体.MAR(也就是地址寄存器).MDR(也就是数据 ...

  3. CSS 针对谷歌浏览器(Chrome) safari的webkit核心浏览器CSS hack

    @media screen and (-webkit-min-device-pixel-ratio:0) { ul#navUL ul a{padding:8px 2px;word-break:keep ...

  4. openstack stein部署手册 2. 基础应用

    1. chrony # 安装程序包 yum install -y chrony # 变更配置文件 /etc/chrony.conf 增加 server 192.168.123.200 iburst # ...

  5. 246-基于TI DSP TMS320C6678、Altera FPGA的CPCI处理卡

    基于TI DSP TMS320C6678.Altera FPGA的CPCI处理卡 1.板卡概述  本板卡由我公司自主研发,基于CPCI架构,符合CPCI2.0标准,采用两片TI DSP TMS320C ...

  6. ltp-ddt eth_iperf_tcp iperf dualtest遇到的问题

    ltp-ddt eth_iperf_tcp server端:iperf -s -i 5 -w 1M client端将ddt的核心代码抠出来: iperf -c 1921.68.40.41 -m -M ...

  7. HTML5 arc的例子

    demo.html <!DOCTYPE html> <html lang="zh"> <head> <meta charset=" ...

  8. 三栏布局只知道圣杯、双飞翼,最终评级是……F

    三栏布局,面试与工作中的常见布局.分左中右三部分,其中左右宽度已知,中间宽度自适应.根据不同的DOM顺序与CSS处理,这里写下了五类布局方式. 一.浮动布局 1 圣杯布局 L:"我问你,你就 ...

  9. 在项目中使用 Maven 私服

    #在项目中使用 Maven 私服 在 Maven settings.xml 中添加 Nexus 认证信息(servers 节点下): <server> <id>nexus-re ...

  10. SpringCloud-Nexfilx

    1.概述 Spring Cloud Netflix provides Netflix OSS integrations for Spring Boot apps through autoconfigu ...