CF_505C Mr. Kitayuta, the Treasure Hunter

C. 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 - 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.

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 test(s)
input
4 10
10
21
27
27
output
3
input
8 8
9
19
28
36
45
55
66
78
output
6
input
13 7
8
8
9
16
17
17
18
21
23
24
24
26
30
output
4
Note

In the first sample, the optimal route is 0  →  10 (+1 gem)  →  19  →  27 (+2 gems)  → ...

In the second sample, the optimal route is 0  →  8  →  15  →  21 →  28 (+1 gem)  →  36 (+1 gem)  →  45 (+1 gem)  →  55 (+1 gem)  →  66 (+1 gem)  →  78 (+1 gem)  → ...

In the third sample, the optimal route is 0  →  7  →  13  →  18 (+1 gem)  →  24 (+2 gems)  →  30 (+1 gem)  → ...


题解:在看到这道题的时候最先想到的是用dfs记忆化存储去解,申请了一维的dp数组,但是很明显我们没有办法判断更新,也就是说当以不同的跳度跳到点i的时候,会出现重复++的情况。参考别人的思路我们申请二维的dp数组,记录从i-1到i的距离。但是步长直接开30000存的话肯定是不行的,又发现,其实走过30000之前,步长的变化不会很大,如果步长每次增加1的话,那么最少1+2+...+n=n(n+1)/2>30000,n<250,即步长变化不会超过250.所以第二维保存相对原始步长的改变量,-250~250,开500就够了,这样就不会MLE了。

dp的做法:定义:dp[i][j]表示点i在上一跳度为length=d+j-250时得到的最大宝石数;(之所以这样定义因为跳度只能在d+/-250左右,我们必须保证这一点)

递推关系:dp[to][j+1]=max(dp[to][j+1],dp[i][x]+save[to]);dp[to][j]=max(dp[to][j],dp[i][x]+save[to]);dp[to][j-1]=max(dp[to][j-1],dp[i][x]+save[to]);

附代码:

#include <bits/stdc++.h>
#define MAX_N 30010
using namespace std;

int n,d,mx;
int result;
int dp[MAX_N][500];
int save[MAX_N];
void dfs(int x,int l,int step);
int main()
{
    //储存右界
    int dmax=0;
    scanf("%d%d",&n,&d);
    memset(save,0,sizeof(save));
    memset(dp,-1,sizeof(dp));
    for( int i = 0; i < n; i++ )
    {
        scanf("%d",&mx);
        save[mx]++;
        dmax=max(mx,dmax);
    }
    //第一个点,初始化result
    result=save[d];
    //从该点进入,该点的上一跳度为d+j-250=d
    dp[d][250]=save[d];
    //左界为d,右界为dmax
    for( int i = d; i <= dmax; i++ )
    {
        for( int j = 500; j >= 0; j-- )
        {
            //偏移量在d+/-250以内
            int length=d+j-250;
            //下面语句保证了从dp[d][]的上一跳度只能是d
            if( dp[i][j] == -1 )
                continue;
            //跳度的下届
            if( length <= 0 )
                break;
            //点的上届
            if( i+length-1 > 30000 )
                continue;
            if( length != 1 )
                dp[i+length-1][j-1]=max(dp[i+length-1][j-1],dp[i][j]+save[i+length-1]);
            dp[i+length][j]=max(dp[i+length][j],dp[i][j]+save[i+length]);
            dp[i+length+1][j+1]=max(dp[i+length+1][j+1],dp[i][j]+save[i+length+1]);
            result=max(max(result,dp[i+length-1][j-1]),max(dp[i+length][j],dp[i+length+1][j+1]));
        }
    }
    printf("%d\n",result);
    return 0;
}
看到学长还有用记忆化存储的方法,定义:dp[i][j]为点i在上一跳度为d+j-250时得到的最大宝石数。思路是一致的,膜拜一下代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>

using namespace std;
#define maxn 30005
#define inf 0x7ffffff
pair <int ,int > p;
int arr[maxn];
int dp[maxn][600];
int n,d;
int num;
int findG(int x,int len);
int main()
{
    scanf("%d%d",&n,&d);
    memset(arr,0,sizeof(arr));
    memset(dp,-1,sizeof(dp));
    int tmp ;
    num = -inf;
    for(int i = 0; i < n; i++)
    {
        scanf("%d",&tmp);
        arr[tmp] ++;
        if(num < tmp)
        {
            num = tmp;
        }
    }
    //0是第一个点findG返回从该点出发能得到的最多宝石数目
    int ans = arr[0];
    ans += findG(d,d);
    printf("%d\n",ans);
    return 0;
}
int findG(int x,int len)
{
    //跳度下界
    if(len <= 0)
    {
        return 0;
    }
    //点上界
    if(x > num)
    {
        return 0;
    }
    //记忆化存储
    if(dp[x][len - d + 300] != -1)
    {
        return dp[x][len - d + 300];
    }
    int left = findG(x+len-1,len-1);
    int mid = findG(x+len,len);
    int right = findG(x+len+1,len+1);
    return dp[x][len - d + 300] = arr[x] + max(mid,max(left,right));
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值