题目链接: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内置函数之staticmethod()

    staticmethod(function)返回函数的静态方法.一般来说,实例对象调用类方法不用传入参数,因为实例对象本身隐式的作为第一个参数传入了.而采用静态方法之后,实例对象在调用类方法时必须传入 ...

  2. git修改用户名以及邮箱

    git 修改当前的project的用户名的命令为:git config user.name 你的目标用户名; git 修改当前的project提交邮箱的命令为:git config user.emai ...

  3. 恢复不小心误覆盖的代码文件 (sublime编辑器)

    最新写了一个python脚本,另外在终端上运行一个shell脚本循环记录一些性能.耗时参数.取出记录的数据,使用python pyplot强大的绘图功能来可视化,易用程度仅次于matlab. 本次文件 ...

  4. python3读取BJDA药品经营企业数据

    #-*- coding:utf-8 -*- #读取北京FDA的药品经营企业数据 # 20161125 zhangshaohua import re import urllib.request impo ...

  5. git入门四(分支创建合并)

    熟悉git分支的原理是掌握了git的精髓,因为git和我们常用的源码管理系统有很大的区别和优点在分支上可以体现出来,一般我们常用的源码管理系统分支都是需要创建新目录,有全新的源码copy,一般都需要创 ...

  6. 几种session存储方式比较

    原文: http://blog.sina.com.cn/s/blog_495697e6010143tj.html 集群中session安全和同步是个最大的问题,下面是我收集到的几种session同步的 ...

  7. STL源代码分析--萃取编程(traits)技术的实现

    1.为什么要出现? 依照默认认定.一个模板给出了一个单一的定义,能够用于用户能够想到的不论什么模板參数!可是对于写模板的人而言,这样的方式并不灵活.特别是遇到模板參数为指针时,若想实现与类型的參量不一 ...

  8. python 基础 9.12 索引

    #/usr/bin/python #-*- coding:utf-8 -*- #@Time   :2017/11/24 4:48 #@Auther :liuzhenchuan #@File   :索引 ...

  9. This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its 错误解决办法

    This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary log ...

  10. docker安装并配置加速

    安装 旧版本的 Docker 称为 docker 或者 docker-engine,使用以下命令卸载旧版本: sudo apt-get remove docker \ docker-engine \ ...