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.
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.
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+7; const double PI = acos(-1.0); const double eps = 1e-6 ; const int N = 30010; const int mod = 2333333; int n , d , x , dp[N][2010] , cnt[N]; int dfs( int i , int l ) { if( i > x ) return 0 ; int tmp = cnt[i]; if( l > 1 ) tmp = max( tmp , cnt[i] + dfs( i + l - 1 , l - 1 ) ); tmp = max( tmp , cnt[i] + dfs( i + l , l ) ); tmp = max( tmp , cnt[i] + dfs( i + l + 1 , l + 1 ) ); return tmp ; } int DP( int i , int l ) { if( i > x ) return 0 ; if( dp[i][l] == -1 ) { int &tmp = dp[i][l] ; tmp = cnt[i]; if( l > 1 ) tmp = max( tmp , cnt[i] + DP( i + l - 1 , l - 1 ) ); tmp = max( tmp , cnt[i] + DP( i + l , l ) ); tmp = max( tmp , cnt[i] + DP( i + l + 1 , l + 1 ) ); } 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 , 0 , sizeof cnt ) ; memset( dp , -1 , sizeof dp ) ; for( int i = 0 ; i < n ; ++i ) { cin >> x ; cnt[x]++; } if( d > 2000 ) cout << dfs( d , d ) << endl ; else cout << DP(d,d) << endl ; } }