题意:有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. http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/

    http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/

  2. [topcoder]BinaryCards

    现在觉得有空时可以刷一下topcoder的DIV 2的Lvl 3的题目.感觉和刷LeetCode和WikiOi都是不一样的. http://community.topcoder.com/stat?c= ...

  3. 蓝牙RSSI计算距离

    利用CoreLocation.framework很容易扫描获得周边蓝牙设备,苹果开源代码AirLocate有具体实现,下载地址: https://developer.apple.com/library ...

  4. C++静态变量对象的建立和删除,兼论MFC开始运行的起点(全局对象)

    看了不少C++书,当讲到静态变量的时候,总是以int成员来举例,是啊,这样很好理解.但是如果这个静态变量是一个对象行不行呢?不仅行,有时候还非常必要,而且别有洞天. 比如: // .h 文件 clas ...

  5. POJ_1182_食物链_[NOI]_(并查集)

    描述  http://poj.org/problem?id=1182 共A,B,C三种动物,A吃B,B吃C,C吃A.给出询问 q : t , x , y , 表示: x 与 y 是同类 ( t==1 ...

  6. Web三维技术:Flash Builder+away3d平台搭建(含演示视频)

    转自:http://www.cnblogs.com/beer/archive/2011/07/08/2101492.html 前言:作为页面中实验设备的显示层,需要一个swf作为显示的UI.虽然可以用 ...

  7. (转载)C++ const成员初始化问题

    (转载)http://www.189works.com/article-45135-1.html Const成员如其它任何成员一样,简单考虑其出现在三个位置:全局作用域.普通函数内部.类里面. 下面请 ...

  8. 如何从PDF文件中提取矢量图

    很多时候我们需要PDF文档中的插图,直接用pdf中的复制或者截屏软件只能提取位图格式的图片,放大缩小难免失真. 本文教大家一种一种从pdf中提取矢量图的方法. 工具软件: 1 adobe acroba ...

  9. 存储过程实例总结(开发中的错误与总结,调试,数据库函数DATEDIFF计算当前日期是否在本周内)

    USE [POND] GO /****** Object: StoredProcedure [dbo].[OrderChargeList] Script Date: 04/16/2014 13:32: ...

  10. php中数组可以不写下标

    <?php $array[10] = 10; for($i = 0; $i < 100; $i++){ $array[] = $i; } var_dump($array) ?> $a ...