题目链接:http://codeforces.com/problemset/problem/505/C

题意:

  有n个宝石,分别在位置p[i]。(1 <= n,p[i] <= 30000)

  初始时你在位置0,第一次走可以往前跳d的距离。

  从第二次跳开始,如果前一次跳的距离是x,这一次跳的距离只能是x-1,x,x+1中的一种。

  没每跳到一个地方,会获得那里的所有宝石。

  问你最多能拿到多少宝石。

题解:

  表示状态:

    dp[i][j] = max gems

    表示初始在位置i,上一次跳的距离为j,在这以及之后所能拿到的最大价值。

  找出答案:

    ans = dp[d][d]

  如何转移:

    dp[i][j] = max dp[i+j-1][j-1]

    dp[i][j] = max dp[i+j][j]

    dp[i][j] = max dp[i+j+1][j+1]

    然而O(N^2)过不了……

    因为每次跳的距离最多变化1,所以当n=30000时,跳的距离变化小于250,需要用到的j∈[d-250, d+250]。

    精确一些就是j∈[d-k, d+k],其中k = ((sqrt(8n+1)-1)/2)+5。

    将所有j映射到[0, 2k]的范围内,这样时间和空间就都能满足了。

  边界条件:

    if(i>maxd) dp[i][j] = 0

    maxd是有宝石的最远距离。

    当i>maxd时,之后不可能有任何收获。

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAX_N 30005
#define MAX_V 1005
#define cal(x) ((x)-d+k) using namespace std; int n,d,k;
int maxd=;
int a[MAX_N];
int dp[MAX_N][MAX_V]; int dfs(int i,int j)
{
if(i>maxd) return ;
if(dp[i][cal(j)]!=-) return dp[i][cal(j)];
if(j->) dp[i][cal(j)]=max(dp[i][cal(j)],dfs(i+j-,j-)+a[i]);
dp[i][cal(j)]=max(dp[i][cal(j)],dfs(i+j,j)+a[i]);
dp[i][cal(j)]=max(dp[i][cal(j)],dfs(i+j+,j+)+a[i]);
return dp[i][cal(j)];
} int main()
{
cin>>n>>d;
memset(a,,sizeof(a));
int x;
for(int i=;i<=n;i++)
{
cin>>x;
a[x]++;
maxd=max(maxd,x);
}
k=(int)((sqrt(n*+)-1.0)/2.0)+;
memset(dp,-,sizeof(dp));
cout<<dfs(d,d)<<endl;
}

Codeforces 505C Mr. Kitayuta, the Treasure Hunter:dp【考虑可用范围】的更多相关文章

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

    题意:有30001个岛,在一条线上,从左到右编号一次为0到30000.某些岛屿上有些宝石.初始的时候有个人在岛屿0,他将跳到岛屿d,他跳跃的距离为d.如果当前他跳跃的距离为L,他下一次跳跃的距离只能为 ...

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

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

  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. Python内置函数之super()

    super(type[,object-or-type]) super()的作用在于类继承方面. 他可以实现不更改类内部代码,但是改变类的父类. 例子: 一般我们继承类的方式: >>> ...

  2. linux 文件夹-文件权限设置

    只设置文件夹权限为755 文件权限为644find -type d -exec chmod 755 {} \;  find -type f -exec chmod 644 {} \;  或者  fin ...

  3. Linux驱动platform

    platform device<==> platform bus <==> platform driver 转自:platform设备驱动全透析 宋宝华 http://blog ...

  4. Sql Server 查询一段日期内的全部礼拜天

    /* 查询一段日期内的全部礼拜天 @startdate 開始日期 @enddate 结束日期 */ declare @startDate datetime declare @endDate datet ...

  5. html5小趣味知识点系列(一)autofocus

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 【BZOJ3779】重组病毒 LCT+DFS序

    [BZOJ3779]重组病毒 Description 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力极强.为了阻止这种病毒传播,某安全机构策 ...

  7. IIS架构介绍

    IIS7及以上版本提供的请求-处理架构包括以下内容: Windows Process Activation Service(WAS)可以让站点支持更多协议,不仅仅是HTTP和HTTPS 可以通过增加或 ...

  8. 如何用excel urldecode解码把url编码转为汉字?

    统计分析可以反映出网站运营的情况,并根据实际作出相应的调整,是站长必需的基础技能.ytkah感觉最好用的是谷歌统计,里面有个搜索关键词及对应受访页面,这个功能对优化用处很大,但大家都知道访问不太顺畅. ...

  9. spark在yarn-cluster上面执行报错

    在单机模式下执行成功的spark程序,在yarn上面就报错.异常信息如下: // :: INFO DAGScheduler: Completed ResultTask(, ) // :: INFO D ...

  10. VS2013 Pro版本密钥

    Visual Studio Professional 2013 KEY(密钥): XDM3T-W3T3V-MGJWK-8BFVD-GVPKY