dp cf C. Mr. Kitayuta, the Treasure Hunter

链接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 - 1l 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.
从d开始,每次可以跳L+1,L-1,L的长度(长度为正数),给定一些岛,求怎么跳才能经过的这些给定的岛最多

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值