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 ;
}
}

最新文章

  1. 数据库 &#39;xxx 的事务日志已满。若要查明无法重用日志中的空间的原因,请参阅 sys.databases 中的 log_reuse_wait_desc 列。
  2. shell脚本: 备份mysql远程数据库并清除一个月之前的数据
  3. UVALive 4670 Dominating Patterns --AC自动机第一题
  4. JQuery AJAX 解析获得的JSON数据
  5. IMX6输出可控PWM
  6. SQL Server调优系列进阶篇 - 如何索引调优
  7. 一段sql的优化
  8. (转)【已解决】关于SQL2008 “不允许保存更改。您所做的更改要求删除并重新创建以下表。您对无法重新创建的标进行了更改或者启用了‘阻止保存要求重新创建表的更改’” 解决方案
  9. listvew加载更多
  10. 回车tab切换
  11. Chapter 2 Open Book——31
  12. 超简单CSS3水平动态进度条+小圆球+背景色渐变
  13. CSS 备忘
  14. UVA 673 Parentheses Balance (栈)
  15. ionic3+angular4开发混合app 之自定义组件
  16. python 闯关之路四(上)(并发编程与数据库理论)
  17. 使用eclipse创建android项目的时候为什么会生成两个项目
  18. Delphi XE10.1 引用计数
  19. 【BZOJ】1875: [SDOI2009]HH去散步 矩阵快速幂
  20. 给Swing的GUI组件设置前景色和背景色

热门文章

  1. 汇编语言之寄存器使用bx si di bp
  2. [JavaScript深入系列]JavaScript深入之执行上下文栈(转载)
  3. android&amp;iOS设计分辨率
  4. 一、免费API调用
  5. kafka2.3集群搭建
  6. MySQL --12 备份的分类
  7. PHP: 双层 for循环的执行过程
  8. 消息队列之AciveMQ
  9. BZOJ1822 [JSOI2010]Frozen Nova 冷冻波 二分+最大流
  10. [BZOJ5073] [Lydsy1710月赛]小A的咒语 后缀数组+dp+贪心