ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

CF 505C(Mr. Kitayuta, the Treasure Hunter-Dp¿¼ÂÇ¿ÉÓ÷¶Î§)(Ò»)
2015-07-20 17:23:21 À´Ô´: ×÷Õß: ¡¾´ó ÖРС¡¿ ä¯ÀÀ:2´Î
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++º¯ÊýÖ¸ÕëÏê½â

ÆÀÂÛ

ÕÊ¡¡¡¡ºÅ: ÃÜÂë: (ÐÂÓû§×¢²á)
Ñé Ö¤ Âë:
±í¡¡¡¡Çé:
ÄÚ¡¡¡¡ÈÝ:

¡¤Spring Boot Java£º (2025-12-26 16:20:19)
¡¤Spring Boot¤ÇHello (2025-12-26 16:20:15)
¡¤Spring ¤Î»ù±¾¤«¤éŒ (2025-12-26 16:20:12)
¡¤C++Ä£°å (template) (2025-12-26 15:49:49)
¡¤C ÓïÑÔÖÐÄ£°åµÄ¼¸ÖÖ (2025-12-26 15:49:47)