CF 505C (Mr. Kitayuta, the Treasure Hunter-Dp considering the available range), 505 ckitayuta

Source: Internet
Author: User

CF 505C (Mr. Kitayuta, the Treasure Hunter-Dp considering the available range), 505 ckitayuta

C. Mr. Kitayuta, the Treasure Huntertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. the islands are evenly spaced along a line and numbered from 0 to 30000 from the west to the east. these islands are known to contain your treasures. there areNGems in the Shuseki Islands in total, andI-Th gem is located on islandPI.

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 islandD.
  • After that, he will continue jumping according to the following rule. LetLBe the length of the previous jump, that is, if his previous jump was from islandPrevTo islandCur, LetLSignature = SignatureCurAccept-Encoding-Prev. He will perform a jump of lengthLExecutor-cores 1,LOrLWhen + then 1 to the east. That is, he will jump to island (CurRegion + RegionLCallback-interval 1 ),(CurRegion + RegionL) Or (CurRegion + RegionLValues + values 1) (if they exist). The length of a jump must be positive, that is, he cannot perform a jump of length 0 whenLVertex = destination 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 integersNAndD(1 digit ≤ DigitN, Bytes,DLimit ≤ limit 30000), denoting the number of the gems in the Shuseki Islands and the length of the Mr. Kitayuta's first jump, respectively.

The nextNLines describe the location of the gems.I-Th of them (1 operator ≤ OperatorILimit ≤ limitN) Contains a integerPI(DLimit ≤ limitP1 bytes ≤ bytesP2 bytes ≤ bytes... bytes ≤ bytesPN  ≤000030000), denoting the number of the island that containsI-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 route → route 10 (+ 1 gem) route → route 19 route → route 27 (+ 2 gems) route → route...

In the second sample, the optimal route is 0 then → listen 8 then → listen 15 then → listen 21 then → listen 28 (+ 1 gem) Then → listen 36 (+ 1 gem) generation → generation 45 (+ 1 gem) generation → generation 55 (+ 1 gem) generation → generation 66 (+ 1 gem) generation → generation 78 (+ 1 gem) else → else...

In the third sample, the optimal route is 0 route → route 7 route → route 13 route → route 18 (+ 1 gem) route → route 24 (+ 2 gems) generation → generation 30 (+ 1 gem) generation → generation...


Dp is simple, but d has a large scope.

However, it can be proved that the final d value cannot exceed 500.

Intercept A Piece Of explanation:

Below is the explanation from yosupo, translated by me.

[From here]

LetMBe the number of the islands (that is, 30001). First, let us describe a solution with time and memory complexityO(M2 ).

We will apply Dynamic Programming. letDp[I] [J] Be the number of the gems that Mr. Kitayuta can collect after he jumps to islandI, When the length of his previous jump isJ(Let us assume that he have not collect the gems on islandI). Then, you can calculate the values of the tableDpBy the following:

  • Dp[I] [J] Rows = else 0, ifILimit ≥ limitM
    (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] Numbers = numbers (the number of the gems on islandI) Accept + acceptMax(Dp[IRegion + RegionJ] [J], RoleDp[IRegion + RegionJPipeline + pipeline 1] [JRows + rows 1]), ifILatency <latencyM, Bytes,JLimit = Limit 1
    (He cannot perform a jump of length 0)
  • Dp[I] [J] Numbers = numbers (the number of the gems on islandI) Accept + acceptMax(Dp[IRegion + RegionJExecutor-cores 1] [JExecutor-cores 1], BuffersDp[IRegion + RegionJ] [J], RoleDp[IRegion + RegionJPipeline + pipeline 1] [JRows + rows 1]), ifILatency <latencyM, Bytes,JLimit ≥ limit 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 valuesJThat we have to consider, which areDAccept-limit 245, limit,DAccept-limit 244, limit,DMaximum-limit 243, maximum..., minimum ,...,DLatency + latency 244 andDLimit + limit 245.

Why? First, let us find the upper boundJ. Suppose Mr. Kitayuta always performs"LCommit + commit 1 "jump (L: The length of the previous jump). Then, he will reach the end of the islands before he performs a jump of lengthDLatency + latency 246, because
DWorker + worker (DRows + rows 1) rows + rows (DPipeline + pipeline 2) pipeline + pipeline... pipeline + pipeline (DLimit + limit 245) limit ≥1 Limit + limit 2 Limit + limit... average + maximum 245 rows = maximum 245 · (245 rows + maximum 1) average/Maximum 2 rows = maximum 30135 rows> maximum 30000. thus, he will never be able to perform a jump of lengthDMemory + Memory 246 or longer.

Next, let us consider the lower boundJIn a similar way. IfDLimit ≤ limit 246, then obviusly he will not be able to perform a jump of lengthDPlease-Skip 246 or shorter, because the length of a jump must be positive. Suppose Mr. Kitayuta always performs"LSkip-commit 1 "jump, whereDLimit ≥ 247. Then, again he will reach the end of the islands before he performs a jump of lengthDCost-limit 246, because
DWorker + worker (DExecutor-cores 1) buffers + buffers (DAccept-limit 2) accept + exceed... Limit + limit (DFaster-than 245) Faster ≥ 245 faster + faster 244 faster + faster... average + maximum 1 rows = maximum 245 · (245 rows + maximum 1) average/Maximum 2 rows = maximum 30135 rows> maximum 30000. thus, he will never be able to perform a jump of lengthDAccept-limit 246 or shorter.

Therefore, we have obtained a working solution: similar toO(M2) one, but we will only consider the valueJBetweenDCost-effectiveness 245 andDCalifornia + California 245. The time and memory complexity of this solution will beO(M1.5), since the value "245" is slightly larger.

This solution can be implemented by, for example, using a "normal" two dimented 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<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<ctime>#include<map>#include<vector> using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  #define Lson (x<<1)#define Rson ((x<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (100000007)#define MAXN (30000+10)#define MAXD (30000+10)#define M (30001)#define MP(a,b) make_pair(a,b) #define MAX_d_change (250+10)#define C (250)long long mul(long long a,long long b){return (a*b)%F;}long long add(long long a,long long b){return (a+b)%F;}long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}typedef long long ll;int n,d,a[MAXN]={0},s[MAXN]={0},f[MAXN][MAX_d_change*2]={0};int main(){//freopen("Treasure.in","r",stdin);//freopen(".out","w",stdout);cin>>n>>d;For(i,n){int p;scanf("%d",&p);a[p]++;}For(i,M) s[i]=s[i-1]+a[i];int ans=0;memset(f,-1,sizeof(f));ans=f[d][C]=a[d];Fork(i,d,M){Rep(j,2*C+1)if (f[i][j]>=0){int dis=j-C+d;if (dis>0&&i+dis<=M) {f[i+dis][j]=max(f[i+dis][j],f[i][j]+a[i+dis]);ans=max(ans,f[i+dis][j]);}if (i+dis+1<=M) {f[i+dis+1][j+1]=max(f[i+dis+1][j+1],f[i][j]+a[i+dis+1]);ans=max(ans,f[i+dis+1][j+1]);}if (dis-1>0&&i+dis-1<=M) {f[i+dis-1][j-1]=max(f[i+dis-1][j-1],f[i][j]+a[i+dis-1]);ans=max(ans,f[i+dis-1][j-1]);}}}cout<<ans<<endl;return 0;}







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.