设为首页 加入收藏

TOP

CF 505C(Mr. Kitayuta, the Treasure Hunter-Dp考虑可用范围)(一)
2015-07-20 17:23:21 来源: 作者: 【 】 浏览:6
Tags:505C Mr. Kitayuta the Treasure Hunter-Dp 考虑 可用 范围

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

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


    这题直接dp很简单,但是d的范围很大

    但是可以证明最终用到的d的取值最多不超过500

    截取一段explanation:

    Below is the explanation from yosupo, translated by me.

    [From here]

    Let m be the number of the islands (that is, 30001). First, let us describe a solution with time and memory complexity of O(m2).

    We will apply Dynamic Programming. let dp[i][j] be the number of the gems that Mr. Kitayuta can collect after he jumps to island i, when the length of his previous jump is j (let us assume that he have not collect the gems on island i). Then, you can calculate the values of the table dp by the following:

    • dp[i][j]?=?0, if i?≥?m
      (actually these islands do not exist, but we can suppose that they exist and when Mr. Kitayuta jumps to these islands, he stops jumping)
    • dp[i][j]?=? (the number of the gems on island i) ?+?max(dp[i?+?j][j],?dp[i?+?j?+?1][j?+?1]), if i? m,?j?=?1
      (he cannot perform a jump of length 0)
    • dp[i][j]?=? (the number of the gems on island i) ?+?max(dp[i?+?j?-?1][j?-?1],?dp[i?+?j][j],?dp[i?+?j?+?1][j?+?1]), if i? m,?j?≥?2

      This solution is unfeasible in terms of both time and memory. However, the following observation makes it an Accepted solution: there are only 491 values of j that we have to consider, which are d?-?245,?d?-?244,?d?-?243,?...,?d?+?244 and d?+?245.

      Why? First, let us

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇BZOJ 2119 股市的预测 后缀数组 下一篇C++函数指针详解

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·如何理解c语言指针和 (2025-12-27 01:19:11)
·为什么C标准库没有链 (2025-12-27 01:19:08)
·玩转C语言和数据结构 (2025-12-27 01:19:05)
·MySQL 基础入门视频 (2025-12-26 23:20:22)
·小白入门:MySQL超详 (2025-12-26 23:20:19)