[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 ;
}
上一篇:Ha ha lou!


下一篇:【Go】 http webserver