Codeforces Round #286 (Div. 2) C. Mr Kitayuta, the Treasure hunter+dp+ optimization

Source: Internet
Author: User

C. Mr Kitayuta, the treasure huntertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutput Standard output

The Shuseki Islands is an archipelago of30001Small islands in the Yutampo Sea. The islands is evenly spaced along a line, numbered from0To30000From the west to the east. These islands is known to contain many treasures. There isNGems 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'll repeatedly perform jumps between islands to the east according to the following PR Ocess:

  • First, he'll jump from the island 0 to the island D.
  • After that, he'll continue jumping according to the following rule. LetLBeing the length of the previous jump, that's, if his previousprevTo Islandcur, let l? =? cur?-? prev . He'll perform a jump of length L?-? 1,LOr l. +?1to the east. That's, he'll jump to the island(cur? +? ) L?-? 1),(cur? +? ) L)Or(cur? +? ) L. +?1)(if they exist). The length of a jump must was positive, that's, he cannot perform a jump of length0When l. =?1. If There is no valid destination, he'll stop jumping.

Mr Kitayuta would 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 the 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 firs T jump, respectively.

The nextNLines describe the location of the gems. TheI-th of them (1?≤? i? ≤? N ) contains a integer Pi ( d? ≤? P 1? ≤? P 2. ≤?...? ≤? P n? ≤?30000), denoting the number of the island that contains theI-th Gem.

Output

Print the maximum number of gems that Mr Kitayuta can collect.

Sample Test (s) input
4 1010212727
Output
3
Input
8 8919283645556678
Output
6
Input
13 788916171718212324242630
Output
4
Note

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

In the second sample, the optimal route is 0? →?8? →?15? →?21st? →?(+1 Gem)? →?(+1 Gem)? →?(+1 Gem)? →?(+1 Gem)? →?(+1 Gem)? →?(+1 Gem)? →?...

in The third sample, the optimal route is 0 ? →?  24 (+2 Gems)   ? →?  30 (+1 Gem)  ? →?...


Solution: This problem I have started from going to post DP, found a serious error, it is not able to quickly find a D as the starting point of the optimal path, should be from the forward DP. Set Dp[i][j],i as the position, J jumps to the step of I from the previous position. From the back forward DP is as follows :

Dp[i][j]=0 when I is greater than n (number of the total island)

Dp[i][j]=i Island +max (dp[i+j][j],dp[i+j+1][j+1]) when j==1&&i<=n

Dp[i][j]=i Island +max (Dp[i+j][j],dp[i+j+1][j+1],dp[i+j-1][j-1]) when j>1&&i<=n

But this still does not, can not open n^2 so big group, time complex is also n^2, will burst memory, so must optimize the dimension.

If each step goes in d+1 step, the total step is: d+1+d+2+d+3+.......+d+245>=1+.....+245=? 245 ( 245?+?1)?/?2?=?30135?>?30000

Therefore, the maximum size is less than d+245 step

If each step of the d>245 is in the d-1 step, the totalstep is: D?+? (D?-? 1)? +? (D?-? 2)? +?...? +? (D?-? 245)? ≥?245?+?244?+?...? +?1?=?245 (245?+?1)?/?2?=?30135?>?30000

The step size is greater than d-245.

So the range is between d-245 to d+245, and the minimum step for d<245 is 1. With these rules, our arrays can be opened in relatively small dp[30003][600].

Code

#include <iostream> #include <cstdio> #include <cstring> #include <cmath>using namespace std;    int Dp[60603][600];int score[30003];int Max;int main () {int n,d;        while (~SCANF ("%d%d", &n,&d)) {memset (score,0,sizeof (score));            for (int i=0; i<n; i++) {int p;            scanf ("%d", &p);        score[p]++;        } memset (Dp,0,sizeof (DP));        max=0;        int mm=sqrt (2*30000);        int st,offset;            if (d-mm<=0) {st=1;        offset=0;            } else {st=d-mm;        Offset=d-mm-1; } for (int i=30000, i>=d; i--) {for (int j=st; j<=d+mm; J + +) {if (                j-1==0) Dp[i][j-offset]=score[i]+max (dp[i+j][j-offset],dp[i+j+1][j-offset+1]); if (j-1>=1) Dp[i][j-offset]=score[i]+max (Dp[i+j+1][j-offset+1],max (dp[i+j][j-offset],dp[i+j-1][j-offset-1          ]));  }} printf ("%d\n", Dp[d][d-offset]); } return 0;}

Codeforces Round #286 (Div. 2) C. Mr Kitayuta, the Treasure hunter+dp+ optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.