题意:有30001个岛,在一条线上,从左到右编号一次为0到30000。某些岛屿上有些宝石。初始的时候有个人在岛屿0,他将跳到岛屿d,他跳跃的距离为d。如果当前他跳跃的距离为L,他下一次跳跃的距离只能为L-1,L,L+1之一且不能为0。他只能往编号更大的岛跳,直到他不能跳,问他最多能收集多少个宝石?

思路:用dp[i][j]表示在第i个岛,上一步跳的距离为j的收集到的最多宝石的个数。这样如果直接表示的话,j最大可能是30000,空间会超,但是所跳跃的距离不会超过d+250, 因为额1+2+3+...+250>30000, 所以如果用偏移量来表示的话,就可以了,dp[i][j]表示在第i个岛,上一步的跳跃的距离为j-250+d,其中d-250算是一个偏移量,因为如果直接用d表示的话,那么如果d减少1,就会出现负数,加上250的偏移就不会是负数了。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = ;
int num[maxn];
int dp[maxn][];
int main()
{
int n, d, p;
scanf("%d%d", &n, &d);
int Max = ;
for (int i = ; i < n; i++)
{
scanf("%d", &p);
num[p]++;
Max = max(Max, p);
}
memset(dp, -, sizeof(dp));
dp[d][] = num[d];
int ans = ;
for (int i = d; i <= Max; i++)
{
for (int j = ; j <= ; j++)
{
if (dp[i][j] != -)//判断第i个岛是否可达,如果可达,才可以进行往后转移(也就是往后跳)
{
ans = max(ans, dp[i][j]);
int step = j - + d;
if (i + step <= Max)//这里不用判断step>0,因为能进来,肯定是满足的step>0的。
dp[i + step][j] = max(dp[i + step][j], dp[i][j] + num[i + step]);
if (step - > && i + step - <= Max)
dp[i + step - ][j - ] = max(dp[i + step - ][j - ], dp[i][j] + num[i + step - ]);
if (i + step + <= Max)
dp[i + step + ][j + ] = max(dp[i + step + ][j + ], dp[i][j] + num[i + step + ]);
}
}
}
printf("%d\n", ans);
return ;
}

codeforces 505C Mr. Kitayuta, the Treasure Hunter(dp)的更多相关文章

  1. [Codeforces 505C]Mr. Kitayuta, the Treasure Hunter

    Description The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. The is ...

  2. Codeforces 505C Mr. Kitayuta, the Treasure Hunter:dp【考虑可用范围】

    题目链接:http://codeforces.com/problemset/problem/505/C 题意: 有n个宝石,分别在位置p[i].(1 <= n,p[i] <= 30000) ...

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

    题目链接: C. Mr. Kitayuta, the Treasure Hunter time limit per test 1 second memory limit per test 256 me ...

  4. Codefores 506A Mr. Kitayuta, the Treasure Hunter( DP && dfs )

    A. Mr. Kitayuta, the Treasure Hunter time limit per test 1 second memory limit per test 256 megabyte ...

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

    题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...

  6. 505C Mr. Kitayuta, the Treasure Hunter

    传送门 题目大意 一共有30000个位置,从第0个位置开始走,第一次走k步,对于每一次走步,可以走上一次的ki+1 ,ki ,ki-1步数(必须大于等于1),每个岛上有value,求最大能得到的val ...

  7. 【codeforces 505C】Mr.Kitayuta,the Treasure Hunter

    [题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...

  8. [Codeforces Round#286] A.Mr. Kitayuta, the Treasure Hunter 【Normal DP..】

    题目链接:CF#286 - A 这场CF就这样爆零了...我真是太蒟蒻了... 题目分析 比赛的时候看到A题就发现不会,之后一直也没想出来,于是就弃了,还好不提交也不掉Rating... 比赛后看评论 ...

  9. cf 506 A. Mr. Kitayuta, the Treasure Hunter

    不知道这个sb题怎么做错了.. /*#include <bits/stdc++.h> #define LL long long using namespace std; inline in ...

随机推荐

  1. Android软件开发之获取通讯录联系人信息

    Android手机的通讯录联系人全部都存在系统的数据库中,如果须要获得通讯里联系人的信息就须要访问系统的数据库,才能将信息拿出来. 这一篇文章我主要带领同学们熟悉Android的通讯录机制. 图中选中 ...

  2. perl 初始化Hash

    Vsftp:/root/perl/6# cat a5.pl use Data::Dumper; my @arr=qw/a bc d /; my %rec=(); for $field (@arr){ ...

  3. C# Web版报表

    1.添加ReportViewer 2.添加ObjectDataSource 3.须添加一个ScriptManager 4.添加并设计一个报表用数据集 5.设计rdlc报表,数据源选择第4步的数据集 6 ...

  4. scp 在不同主机之间数据传输

    不同的Linux之间copy文件常用有3种方法,第一种就是ftp,也就是其中一台Linux安装ftp Server,这样可以另外一台使用ftp的client程序来进行文件的copy.第二种方法就是采用 ...

  5. jQuery获取属性之自己遇到的问题

    刚开始是这种写法  用的 attr  结果获取不到 if($("#reg_username_span").attr("display") == 'block') ...

  6. IO流的应用————小型资源管理器

    小型资源管理器 private void LoadTreeView() { DirectoryInfo dir = new DirectoryInfo(@"E:\"); Direc ...

  7. 理解newid()和newsequentialid()

    原文地址:http://blog.csdn.net/xushichang/article/details/4390957 1.:newsequentialid 函数比起 newid 函数最大的好处是: ...

  8. AsyncSocket的使用

    AsyncSocket使用流程 安装AsyncSocket 拷贝AsyncSocket类到项目 使用AsyncSocket set delegate @interface NetWork : NSOb ...

  9. HTML5 Canvas核心技术—图形、动画与游戏开发.pdf1

    canvas元素可以说是HTML5元素中功能最强大的一个,它真正的能力是通过Canvas的context对象(绘图上下文)表现出来的 fillText()方法使用fillStyle属性来填充文本中的字 ...

  10. 28个Unix/Linux的命令行神器

    下面是Kristóf Kovács收集的28个Unix/Linux下的28个命令行下的工具(原文链接),有一些是大家熟悉的,有一些是非常有用的,有一些是不为人知的.这些工具都非常不错,希望每个人都知道 ...