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:
Mr. Kitayuta will collect the gems on the islands visited during the process. Find the maximum number of gems that he can collect.
InputThe 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.
OutputPrint the maximum number of gems that Mr. Kitayuta can collect.
Sample test(s) input4 10 10 21 27 27output
3input
8 8 9 19 28 36 45 55 66 78output
6input
13 7 8 8 9 16 17 17 18 21 23 24 24 26 30output
4Note
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) ?→?...
但是可以證明最終用到的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:
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 find the upper bound of j. Suppose Mr. Kitayuta always performs the "l?+?1"
jump (l: the length of the previous jump). Then, he will reach the end of the islands before he performs a jump of length d?+?246,
because
d?+?(d?+?1)?+?(d?+?2)?+?...?+?(d?+?245)?≥?1?+?2?+?...?+?245?=?245·(245?+?1)?/?2?=?30135?>?30000. Thus, he will never be able to
perform a jump of length d?+?246 or longer.
Next, let us consider the lower bound of j in a similar way. If d?≤?246,
then obviously he will not be able to perform a jump of length d?-?246 or shorter, because the length of a jump must be positive. Suppose Mr. Kitayuta always
performs the "l?-?1" jump, where d?≥?247. Then,
again he will reach the end of the islands before he performs a jump of length d?-?246, because
d?+?(d?-?1)?+?(d?-?2)?+?...?+?(d?-?245)?≥?245?+?244?+?...?+?1?=?245·(245?+?1)?/?2?=?30135?>?30000. Thus, he will never be able
to perform a jump of length d?-?246 or shorter.
Therefore, we have obtained a working solution: similar to the O(m2) one,
but we will only consider the value of j between d?-?245 andd?+?245.
The time and memory complexity of this solution will be O(m1.5),
since the value "245" is slightly larger than .<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+ClRoaXMgc29sdXRpb24gY2FuIGJlIGltcGxlbWVudGVkIGJ5LCBmb3IgZXhhbXBsZSwgdXNpbmcgYSA="normal" two dimensional array with a offset like this: dp[i][j
- offset]
. The time limit is set tight in order to fail most of naive solutions with search using std::map or something, so using hash maps (unordered_map) will be risky although the complexity will be the same as the described solution.
[End]
#include#include #include #include #include #include #include #include #include #include