题目链接: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. 前台freemark获取后台的值

    1.后台代码: ModelAndView mv = new ModelAndView("log/logList.ftl"); String info="abc" ...

  2. oracle中避免sort操作

    1.升序排列 create index ix_table1 on table1 (column1,column2); select column1,column2 from table1 order ...

  3. 搭建redis集群遇到的坑

    搭建redis集群遇到的坑 #!/bin/bash # 作者: tuhooo # 日期: 2017.4.23 20.15 # 用途: 通过ruby脚本启动redis伪集群 if [ $2 == &qu ...

  4. iOS系列译文:整洁的表视图代码

    本文由 伯乐在线 - christian 翻译自 Florian Kugler.欢迎加入技术翻译小组.转载请参见文章末尾处的要求. 表视图是一个非常万能的iOS应用程序构建模块.因此,有很多与表视图直 ...

  5. Linux Samba文件共享服务配置

    http://blog.csdn.net/xht555/article/details/4631063

  6. Jenkins maven仓库地址 和 手动修改maven 版本

    ① Jenkins maven仓库地址,一般情况会在:/root/.m2/repository/* ② 手动修改maven 版本,Apache 下载指定的maven版本,然后解压后copy到指定目录即 ...

  7. vue-cli (vue脚手架)

    vue-cli(脚手架):它可以自动生成目录 1.在网速不佳的情况下可以安装cnpm(淘宝镜像)如果网速快可以不用安装cnpm直接进行下一步操作 第一步:在命令行执行(全局安装cnpm) npm in ...

  8. POJ 1518 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】

    链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  9. x-www-form-urlencoded名字的由来

    1 提交的是表单数据 所以用form. 2 提交的形式是以参数放在url后面的形式提交的 例如,以x1=y1&x2=y2&x3=y3的形式放在url后面的形式提交,所以是urlenco ...

  10. php读写csv、xml文件: SimpleExcel

    实例结构: 1. csv2xml.demo.php <?php use SimpleExcel\SimpleExcel; // 这句不能少! require_once ('../lib/Simp ...