CodeForces #505C# Mr. Kitayuta, the Treasure Hunter(dp数组定义)

11 篇文章 0 订阅
Description

The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. The islands are evenly spaced along a line, numbered from0 to 30000 from the west to the east. These islands are known to contain many treasures. There aren gems in the Shuseki Islands in total, and thei-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 islandprev to island cur, letl = cur - prev. He will perform a jump of lengthl - 1, l orl + 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 length0 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. Thei-th of them (1 ≤ i ≤ n) contains a integerpi (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 Input

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

Hint

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) → ...


解题报告:题目大意是有片30001宽的海,看作是一条直线,上面分布着好多宝藏。现在给定宝藏的位置(0< x < 30001),以及挖宝者的跳跃能力l,但是规定挖宝者每次只能跳和上一次一样,或者多1,或者少1。问要怎么跳才能获得最多宝藏。

想到动态规划,dp[i][j]表示挖宝藏在i的位置,前一步的步长为j的获得的最大宝藏数。那么就可以得到dp方程;

dp[i][j] = max(dp[i][j], dp[i-j][j], dp[i-j-1][j+1], dp[i-j+1][j-1]) + gem[i];当然边界问题要稍微做一下处理。

但是你会发现数组就是dp[30001][30001]会MLE,所以就需要一个转换。就是后面一维只用来记录偏移量。偏移量的范围差不多是(-250,250),原因是即使弹跳力为0,每次正向偏移1,偏移250次后,1+2+……+250 > 30000了,所以偏移不到250次。负向也是,所以区间可以开(-250,250)。可是数组下标又不能是负数。所以原点设定为250.那么0就是-250,500就是250.所以步长可以表示为j - 250 + d。其他和原来的dp是一样的。

<span style="font-size:14px;">#include <bits/stdc++.h>
using namespace std;
const int oo = 0x7fffffff;
int gem[30001], dp[30001][505], x;
int main() {
    int n, d, ans, maxdis = 0;
    scanf("%d%d", &n, &d);
    for (int i = 0; i < n; i ++) {
        scanf("%d", &x);
        maxdis = max(maxdis, x);
        gem[x] ++;
    }
    ans = gem[d];
    memset(dp, 0x80, sizeof(dp));
    dp[d][250] = gem[d]; //步长是j - 250 + d, 起点是d.
    for (int i = d + 1; i <= maxdis; i ++) {
        for (int j = 1; j < 505; j ++) {
            if (i - (j - 250 + d) < 0 || i - (j - 250 + d) >= i) continue;
            dp[i][j] = max(dp[i][j], dp[i - (j - 250 + d)][j]);
            dp[i][j] = max(dp[i][j], dp[i - (j - 250 + d)][j+1]);
            dp[i][j] = max(dp[i][j], dp[i - (j - 250 + d)][j-1]);
            dp[i][j] += gem[i];
            ans = max(ans, dp[i][j]);
        }
    }
    printf("%d\n", ans);
    return 0;
}</span>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值