Codeforces 505C. Mr Kitayuta, the Treasure Hunter (memory search)

Source: Internet
Author: User

The main topic: there are 30,000 islands from left to right, give you a n a d,n for n a gem, the next n lines indicate which island each gem is on, and D for the first time you jump from 0 to the position, and then each time you can jump from your position l-1,l,l+1 distance.

Problem-solving ideas, in fact, had done a similar, he jumped the number of steps is actually very small, the solution set each jump step plus has been (n+1) XN/2 = 30000 about 250, that is, every time he will jump out of 250 situations, so, we can open dp[30010][500] Add an offset, so that the memory of the search for each point, similar to the tree DP from the root node to find a longest chain, each time Three forks, one is L-1, L, l+1 three direction down looking.

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

The Shuseki Islands is an archipelago of 30001 small Islands in the Yutampo Sea. The islands is evenly spaced along a line, and numbered from0 to 30000 from the west to the east. These islands is known to contain many treasures. There ben gems in the Shuseki Islands in total, and theI-th Gem is located on Island p I.

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 the Mr Kitayuta ' s F Irst 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?≤?...? ≤? P n≤?30000), denoting the number of the island that contains the i-th g Em.

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? →? ? → ?? → (+1 gem)? →?< /c8> (+1 gem), → (+1 gem), → ( +1 gem), → +1 (+1 gem),→? c13>?→?...

In the third sample, the optimal route is 0 ?→? 7?→? ?→? (+1 Gem) ?→? (+2 Gems) ?→? (+1 Gem) ?→?...

#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include < iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include < stack> #include <ctime> #include <map> #include <set> #define EPS 1e-9///#define M 1000100///#define ll __int64#define ll Long long///#define INF 0x7ffffff#define inf 0x3f3f3f3f#define PI 3.1415926535898#define Zero (x) ((FA BS (x) <eps)? 0:x) #define MOD 1000000007#define Read () freopen ("Autocomplete.in", "R", stdin) #define Write () freopen ("    Autocomplete.out "," w ", stdout) #define CIN () Ios::sync_with_stdio (false) using namespace Std;inline int Read () {char ch;    BOOL flag = FALSE;    int a = 0; while (!) ( ((ch = getchar ()) >= ' 0 ') && (ch <= ' 9 ')) | |    (ch = = '-')));        if (ch! = '-') {a *= 10;    A + = ch-' 0 ';    } else {flag = true; } while (((ch = getchar ()) >= ' 0 ') && (ch <= ' 9 ')) {        A *= 10;    A + = ch-' 0 ';    } if (flag) {a = A; } return A;        void write (int a) {if (a < 0) {Putchar ('-');    A =-A;    } if (a >=) {write (A/10); } putchar (a% 10 + ' 0 ');} const int MAXN = 30010;int Dp[maxn][520];int dx[] = {-1, 0, 1};int max;int num[maxn];int sum[maxn];int d;int dfs (int pos,    int l) {int site = Pos+l+d;    if (L+d < 1 | | site > Max) {return 0;    } if (dp[site][l+250]! =-1) return dp[site][l+250];        if (l+d <= 2) {dp[site][l+250] = Sum[site];    return dp[site][l+250];        } if (l+d = = 3) {dp[site][l+250] = Sum[site+2]+num[site];    return dp[site][l+250];    } int sum = 0;        for (int i = 0; i < 3; i++) {int xl = l+dx[i];    sum = max (sum, DFS (site, XL));    } sum + = Num[site]; return dp[site][l+250] = sum;}    int main () {int n;    CIN >>n>>d;    memset (num, 0, sizeof (num)); Memset (DP,-1, SizeoF (DP));    memset (sum, 0, sizeof (sum));    int x;    Max = 0;        for (int i = 0; i < n; i++) {scanf ("%d", &x);        max = max (x, max);    num[x]++;    } for (int i = Max; I >= 1; i--) sum[i] = Sum[i+1]+num[i];    x = DFS (0, 0);    cout<<x<<endl; return 0;}


Codeforces 505C. Mr Kitayuta, the Treasure Hunter (memory search)

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.