codeforces 505C C. Mr. Kitayuta, the Treasure Hunter (dp)

题目链接:

codeforces 505C


题目大意:

给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石。


题目分析:

  • 首先容易想到最暴力的方法:
  • 定义状态dp[i][j]代表到达i位置上一步的大小是j的情况下最多捡到的宝石数。
  • 按照题意模拟即可
  • 但是这样在时间和空间上都是不被允许的
  • 机智的人会发现,因为只有30000个点,所以步幅的变化范围上下不会超过250。
  • 那么暴力搞就好了,我的挫代码就不上了,网上找到了一个黑科技的代码,相当简洁…..

AC代码(非本人代码)

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
const int mod=100000000;
const int INF=0x3f3f3f3f;
const double eqs=1e-8;

int dp[31000][300], w[31000], max1, max2;
int dfs(int cur, int lenth)
{
        if(cur>max1) return 0;
        if(dp[cur][lenth]!=-1) return dp[cur][lenth];
        if(lenth>1) dp[cur][lenth]=max(dp[cur][lenth],dfs(cur+lenth-1,lenth-1)+w[cur]);
        dp[cur][lenth]=max(dp[cur][lenth],dfs(cur+lenth,lenth)+w[cur]);
        dp[cur][lenth]=max(dp[cur][lenth],dfs(cur+lenth+1,lenth+1)+w[cur]);
        max2=max(max2,dp[cur][lenth]);
        return dp[cur][lenth];
}
int main()
{
        int n, d, i, j, x, len, pre;
        max1=-1;
        scanf("%d%d",&n,&d);
        memset(w,0,sizeof(w));
        memset(dp,-1,sizeof(dp));
        for(i=0; i<n; i++) {
                scanf("%d",&x);
                w[x]++;
                max1=max(max1,x);
        }
        max2=0;
        dfs(d,d);
        printf("%d\n",max2);
        return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值