题意:有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. UVA 10034 Freckles 最小生成树

    虽然是道普通的最小生成树题目,可还是中间出了不少问题,暴露的一个问题是不够细心,不够熟练.所以这篇博客就当记录一下bug吧. 代码一:kruskal #include<stdio.h> # ...

  2. 尚未在 Web 服务器上注册 ASP.NET 4.0” 的解决办法

    http://www.sowsoy.com/topics-537.html win7,vs2010创建.NetFramework 4框架下的Asp.Net空网站.系统提示 “尚未在 Web 服务器上注 ...

  3. jackson 常见问题

    org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type   org.codehaus ...

  4. PHP重构之函数上移

    参考<重构> <?php abstract class Customer { public function addBill($date, $amount) { echo " ...

  5. 排序算法_MergeSort

    算法思想: 分治自顶而下实现归并排序: 分治法的三个步骤     设归并排序的当前区间是R[low..high],分治法的三个步骤是:①分解:将当前区间一分为二,即求分裂点               ...

  6. [App]Xamarin学习资料收集

    在博客园其实有很多朋友都在介绍Xamarin的使用方法,下面是比较活跃的一些: http://www.cnblogs.com/yaozhenfa/

  7. [CODEVS1294]全排列

    题目描述 Description 给出一个n, 请输出n的所有全排列 输入描述 Input Description 读入仅一个整数n   (1<=n<=10) 输出描述 Output De ...

  8. Bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 广搜,深搜

    1687: [Usaco2005 Open]Navigating the City 城市交通 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 122  So ...

  9. 玩玩hibernate

    这几天师兄,让我玩玩hibernate,然后通过这个玩意写爬虫(spider).这一说不打紧,嗯,一个星期没有了,全都是由于配置环境,心很塞,整个星期的空闲时间都用来做重复的工作.在学习之前,我先查找 ...

  10. Why do we need smart pointer and how to implement it.

    Here are two simple questions. Problem A #include <string> include <iostream> using name ...