505C Mr. Kitayuta, the Treasure Hunter
题目大意
一共有30000个位置,从第0个位置开始走,第一次走k步,对于每一次走步,可以走上一次的ki+1 ,ki ,ki-1步数(必须大于等于1),每个岛上有value,求最大能得到的value能有多少。
分析
首先我们不难想到dpij表示走到第i个位置,上次走的步数为j,然而30000*30000时间复杂度和空间复杂度都会爆炸,所以我们考虑如何优化掉一维,然而我们发现是无法优化掉一维的。
但由于一个只有30000个位置,所有我们想到上次走的步数的所有可能情况可能不是很多,由式子d+(d+1)+(d+2)+…+(d+maxl)≤30000可得maxl大致是250,也就是说上次走的步数最少是d-250,最多是d+250.于是我们得到dpij,其中i表示考虑到第i个位置,j表示上次走的长度-d,注意为了防止数组出现负数我们将第二维统一加上maxl。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define sp cout<<"---------------------------------------------------"<<endl;
const int inf=1e9+;
int dp[][],maxl=,cnt[],N=;
int main(){
int n,m,i,j,k,d;
scanf("%d%d",&n,&d);
for(i=;i<=n;i++)scanf("%d",&m),cnt[m]++;
for(i=;i<=N;i++)for(j=;j<=;j++)dp[i][j]=-inf;
dp[d][maxl]=cnt[d]+cnt[];
for(i=d;i<N;i++)for(j=-maxl;j<=maxl;j++)if(dp[i][j+maxl]!=-inf){
if(i+j+d<=N&&j+d>)
dp[i+j+d][j+maxl]=max(dp[i+j+d][j+maxl],dp[i][j+maxl]+cnt[i+j+d]);
if(j+d->&&i+j+d-<=N&&j->=-maxl)
dp[i+j+d-][j-+maxl]=max(dp[i+j+d-][j-+maxl],dp[i][j+maxl]+cnt[i+j+d-]);
if(j+d+>&&i+j+d+<=N&&j+<=maxl)
dp[i+j+d+][j++maxl]=max(dp[i+j+d+][j++maxl],dp[i][j+maxl]+cnt[i+j+d+]);
}
int ans=;
for(i=;i<=N;i++)for(j=-maxl;j<=maxl;j++)ans=max(ans,dp[i][j+maxl]);
cout<<ans<<endl;
return ;
}
505C Mr. Kitayuta, the Treasure Hunter的更多相关文章
- [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 Mr. Kitayuta, the Treasure Hunter(dp)
题意:有30001个岛,在一条线上,从左到右编号一次为0到30000.某些岛屿上有些宝石.初始的时候有个人在岛屿0,他将跳到岛屿d,他跳跃的距离为d.如果当前他跳跃的距离为L,他下一次跳跃的距离只能为 ...
- Codeforces 505C Mr. Kitayuta, the Treasure Hunter:dp【考虑可用范围】
题目链接:http://codeforces.com/problemset/problem/505/C 题意: 有n个宝石,分别在位置p[i].(1 <= n,p[i] <= 30000) ...
- 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 505C】Mr.Kitayuta,the Treasure Hunter
[题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...
- Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP
题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...
- [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 ...
随机推荐
- java学习笔记 --- IO(2)
IO流的分类: 流向: 输入流 读取数据 输出流 写出数据 数据类型: 字节流 字节输入流 读取数据 InputStream 字节输出流 写出数据 OutputStream 字符流 字符 ...
- 【SQL查询】查询的值为空时,给出默认值_NVL函数
格式为: NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值,否则返回string1的值. 引申一下,此NVL的作 ...
- c++ 修改stl set中的元素
set的迭代器it有const修饰符,那么对它元素的修改就必然不能成功了.但是有时候遇到要修改stl set元素的问题,这个问题一般的解决方法是先erase这个元素,然后再insert,这样效率很低, ...
- python使用 db.select 返回的数据只能遍历一次
python中通过find从mongo中查出的数据,或者通过select返回的数据,其实返回的是游标,当你进行便利一次之后,游标指向最后, 所以当你再一次进行便利时,便出现数据为空的现象. 解决办法: ...
- Zijian-lv #3 树句节狗提
如你所见,这是一道狗题 一棵树,多次询问与一个点距离至少为 $k$ 的点的权值和 $n,q \leq 2525010$ sol: 长链剖分 需要注意的是这道题卡空间 我把我所有的 vector 换成链 ...
- UVA - 1252 Twenty Questions (状压dp+vis数组加速)
有n个物品,每个物品有m个特征.随机选择一个物品让你去猜,你每次可以询问一个特征的答案,问在采取最优策略时,最坏情况下需要猜的次数是多少. 设siz[S]为满足特征性质集合S的特征的物品总数,dp[S ...
- poj3171 Cleaning Shifts[DP]
https://vjudge.net/problem/POJ-3171.(有价值的区间全覆盖问题) (lyd例题)朴素DP很好想,$f[i]$表示将右端点从小到大排序后从$L$(要求覆盖的大区间)到第 ...
- Qt Creator 中自动补全的快捷键描述
在TextEdit中的completeThis
- spark远程调试
基本流程1.远程运行spark,打开Spark master机器的JVM的jdwp,让其阻塞监听指定端口(8888),让其有终端向指定端口发送特定请求再执行:2.IntelliJ配置socket远程连 ...
- Xaml中string(字符串)常量的定义以及空格的处理
(1)基本用法 xaml中可以实例化各种对象,比如在ResourceDictionary中定义字符串常量: <ResourceDictionary xmlns="http://sche ...