Codeforces 505C Mr. Kitayuta, the Treasure Hunter:dp【考虑可用范围】
题目链接: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【考虑可用范围】的更多相关文章
- codeforces 505C Mr. Kitayuta, the Treasure Hunter(dp)
题意:有30001个岛,在一条线上,从左到右编号一次为0到30000.某些岛屿上有些宝石.初始的时候有个人在岛屿0,他将跳到岛屿d,他跳跃的距离为d.如果当前他跳跃的距离为L,他下一次跳跃的距离只能为 ...
- [Codeforces 505C]Mr. Kitayuta, the Treasure Hunter
Description The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. The is ...
- 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 ...
- 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 ...
- Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP
题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...
- 505C Mr. Kitayuta, the Treasure Hunter
传送门 题目大意 一共有30000个位置,从第0个位置开始走,第一次走k步,对于每一次走步,可以走上一次的ki+1 ,ki ,ki-1步数(必须大于等于1),每个岛上有value,求最大能得到的val ...
- 【codeforces 505C】Mr.Kitayuta,the Treasure Hunter
[题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...
- [Codeforces Round#286] A.Mr. Kitayuta, the Treasure Hunter 【Normal DP..】
题目链接:CF#286 - A 这场CF就这样爆零了...我真是太蒟蒻了... 题目分析 比赛的时候看到A题就发现不会,之后一直也没想出来,于是就弃了,还好不提交也不掉Rating... 比赛后看评论 ...
- cf 506 A. Mr. Kitayuta, the Treasure Hunter
不知道这个sb题怎么做错了.. /*#include <bits/stdc++.h> #define LL long long using namespace std; inline in ...
随机推荐
- [译]GLUT教程 - 键盘
Lighthouse3d.com >> GLUT Tutorial >> Input >> Keyboard GLUT可以让应用程序自动监测键盘输入,包括普通按键和 ...
- AjaxPro.2.dll AjaxPro.AjaxMethod 前后台交互
我们需要下载 AjaxPro.2.zip.然后把下载到的 AjaxPro.2.dll 的文件引入到项目. 1.接着,在 Web.config 的 <system.web> 标签下写入以下内 ...
- JQ中find()与filter()的区别
刚开始学的时候,对于find()和filter()有点理不清楚,下面通过案例相信就可以很快的区分清楚 以下是代码 find弹出的是 filter()弹出的是 下面我们添加div的class是rain ...
- Python读取word文档(python-docx包)
最近想统计word文档中的一些信息,人工统计的话...三天三夜吧 python 不愧是万能语言,发现有一个包叫做 docx,非常好用,具体查看官方文档:https://python-docx.read ...
- TBSchedule源码阅读1-TBScheduleManagerFactory
TBSchedule 1 TBScheduleManagerFactory 初始化 成员变量 ZKManager; IScheduleDataManager; Schedule ...
- windows db2 添加用户权限
http://www.csharpwin.com/csharpspace/12086r9069.shtml 在windows上DB2数据库安装的时候会创建一个系统管理员 的账户,默认为DB2ADMIN ...
- Unity 插件收集(持续更新)
MGS Machinery Unity绑定机械关节,铰链,机构插件包. MGS Mechanical Drive 用于绑定场景中的机械驱动器的Unity插件 Unity Wave Propa ...
- Spring 定时作业
Spring定时任务的几种实现 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我 ...
- EasyDarwin开源流媒体服务器Golang版本:拉转推功能之拉流实现方法
EasyDarwin开源流媒体服务器(www.easydarwin.org),拉转推是一个很有意义的功能,它可将一个独立的RTSP数据源"拉"到服务器,再通过转发协议转发给多个客户 ...
- go语言之面向对象一
在Go语言中, 你可以给任意类型(包括内置类型,但不包括指针类型)添加相应的办法.示例如下: type Integer int func (a Integer) Less(b Integer) boo ...