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 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 ;
}
}
上一篇:Android Handler Message总结


下一篇:1004 Let the Balloon Rise