链接http://codeforces.com/problemset/problem/505/C
题意:
- 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.
想到dp,由于前后每次跳都与上次跳的长度有关,想到状态:dp[i][j]表示跳到第i处,上次跳的长度为j时的最大值
但是由于数据范围为0-30000,感觉这样好像又不行,,
但实际的跳的长度的范围是d-245到d+245
下面给出证明:
那么长度的范围变成d-245到d+245
为方便扩大为d-250到d+250
我们就可以用1到500大致来表示这个区间
每次的跳的长度为j
那么转化为实际的跳的长度为dis=j-250+d
下一步的长度可以为pre+dis,pre+dis-1,pre+dis+1
跳的长度为j,j-1,j+1
要注意中间dis的范围和pre+dis,pre+dis-1,pre+dis+1的范围
下面给出代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAX 30005
int dp[MAX*2][505];
int mp[MAX];
int main(void){
int imax = 0;
int n,d;cin >> n >> d;
for(int i = 1;i<=n;i++){
int t;cin >> t;
mp[t]++;
}
memset(dp,-1,sizeof(dp));
dp[d][250]=mp[d];
imax = mp[d];
for(int i = d; i <= 30000;i++)
for(int j= 1;j <= 500;j++){
if(dp[i][j] != -1){
int to = i+j+d-250;
if(j+d-250 > 0 && to <= 30000)dp[to][j]=max(dp[to][j],dp[i][j]+mp[to]),imax = max(imax,dp[to][j]);
if(j+d-250-1> 0 && to -1 <= 30000)dp[to-1][j-1]=max(dp[to-1][j-1],dp[i][j]+mp[to-1]),imax=max(imax,dp[to-1][j-1]);
if(j+d-250+1> 0 && to + 1<=30000)dp[to+1][j+1]=max(dp[to+1][j+1],dp[i][j]+mp[to+1]),imax=max(imax,dp[to+1][j+1]);
}
}
cout << imax;
}