Codeforces Round #286 Div.1 A Mr Kitayuta, the Treasure Hunter--DP

Source: Internet
Author: User

Test instructions: 0~30000 There are 30,001 places, each place has one or more gold coins, the first step to D, the step size is D, the step after walk can be the last step +1,-1 or unchanged, go to a place to collect the wealth of that place, and now ask to go out (>30000) The maximum amount of wealth that can be collected before.

Solution: It is easy to think of Dp,dp[i][j] to arrive at I, now the step is J when the most collected wealth, transfer is not difficult, cnt[i] represents the wealth I place.

DP[I+STEP-1] = max (dp[i+step-1],dp[i][j]+cnt[i+step+1])

Dp[i+step] = max (Dp[i+step],dp[i][j]+cnt[i+step])

DP[I+STEP+1] = max (dp[i+step+1],dp[i][j]+cnt[i+step+1])

But the step directly open 30000 saved words is certainly not, and found that, in fact, through 30000 before, step changes will not be very large, if the step size increases each time 1, then the minimum 1+2+...+n=n (n+1)/2 > 30000, n< 250, that is, the step change will not exceed 250. So the second dimension to save relative to the original step change amount, -250~250, open 500 is enough, so will not mle.

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>using namespacestd;#defineN 10007intdp[3*n][507];intcnt[3*N],ans;intMain () {inti,j,n,d;  while(SCANF ("%d%d", &n,&d)! =EOF) {memset (CNT,0,sizeof(CNT));  for(i=1; i<=n;i++) {scanf ("%d",&j); CNT[J]++; } memset (DP,-1,sizeof(DP)); dp[d][ -] =Cnt[d]; Ans= dp[d][ -];  for(i=d;i<=30000; i++) {             for(j=1; j<= -; j + +) {                if(Dp[i][j] = =-1)Continue; intto = i+d+j- -; if(To <=30000) {Dp[to][j]= Max (dp[to][j],dp[i][j]+Cnt[to]); Ans=Max (ans,dp[to][j]); }                if(to+1<=30000) {dp[to+1][j+1] = max (dp[to+1][j+1],dp[i][j]+cnt[to+1]); Ans= Max (ans,dp[to+1][j+1]); }                if(To <=30000&& to-i >1) {dp[to-1][j-1] = max (dp[to-1][j-1],dp[i][j]+cnt[to-1]); Ans= Max (ans,dp[to-1][j-1]); } }} cout<<ans<<Endl; }    return 0;}
View Code

Codeforces Round #286 Div.1 A Mr Kitayuta, the Treasure Hunter--DP

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.