codeforces 505C C. Mr. Kitayuta, the Treasure Hunter(dp)
题目链接:
C. Mr. Kitayuta, the Treasure Hunter
1 second
256 megabytes
standard input
standard output
The Shuseki Islands are an archipelago of 30001 small islands in the Yutampo Sea. The islands are evenly spaced along a line, numbered from 0 to 30000 from the west to the east. These islands are known to contain many treasures. There are n gems in the Shuseki Islands in total, and the i-th gem is located on island pi.
Mr. Kitayuta has just arrived at island 0. With his great jumping ability, he will repeatedly perform jumps between islands to the east according to the following process:
- First, he will jump from island 0 to island d.
- After that, he will continue jumping according to the following rule. Let l be the length of the previous jump, that is, if his previous jump was from island prev to island cur, let l = cur - prev. He will perform a jump of length l - 1, l or l + 1 to the east. That is, he will jump to island (cur + l - 1), (cur + l) or (cur + l + 1) (if they exist). The length of a jump must be positive, that is, he cannot perform a jump of length 0 when l = 1. If there is no valid destination, he will stop jumping.
Mr. Kitayuta will collect the gems on the islands visited during the process. Find the maximum number of gems that he can collect.
The first line of the input contains two space-separated integers n and d (1 ≤ n, d ≤ 30000), denoting the number of the gems in the Shuseki Islands and the length of the Mr. Kitayuta's first jump, respectively.
The next n lines describe the location of the gems. The i-th of them (1 ≤ i ≤ n) contains a integer pi (d ≤ p1 ≤ p2 ≤ ... ≤ pn ≤ 30000), denoting the number of the island that contains the i-th gem.
Print the maximum number of gems that Mr. Kitayuta can collect.
4 10
10
21
27
27
3
8 8
9
19
28
36
45
55
66
78
6
13 7
8
8
9
16
17
17
18
21
23
24
24
26
30
4 题意: 有30000个点,n个点放在pi上,第一次去d,每次走的距离只能为上次行走的距离l,l+1,l-1,但是一定要前进;问最后能得到的最大值是多少; 思路: 可以发现行走长度的变化范围不超过250;dp[i][j]表示走j长到达i能得到的最大值;转移方程看代码吧; AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=3e4+;
const int N=3e4;
const int inf=;
int n,d;
int p[maxn],num[maxn],dp[maxn][];
int main()
{
scanf("%d%d",&n,&d);
for(int i=;i<=n;i++)
{
scanf("%d",&p[i]);
num[p[i]]++;
}
memset(dp,-inf,sizeof(dp));
dp[d][]=num[d];
for(int i=;i<=N;i++)
{
for(int j=;j<=;j++)
{
int x=j-+d;
if(i+x>i&&i+x<=N)dp[i+x][j]=max(dp[i+x][j],dp[i][j]+num[i+x]);
if(i+x+>i&&i+x+<=N)dp[i+x+][j+]=max(dp[i+x+][j+],dp[i][j]+num[i+x+]);
if(i+x->i&&i+x-<=N)dp[i+x-][j-]=max(dp[i+x-][j-],dp[i][j]+num[i+x-]);
}
}
int ans=;
for(int i=;i<=N;i++)
{
for(int j=;j<=;j++)
{
ans=max(ans,dp[i][j]);
}
}
cout<<ans<<"\n"; }
codeforces 505C C. Mr. Kitayuta, the Treasure Hunter(dp)的更多相关文章
- 【codeforces 505C】Mr.Kitayuta,the Treasure Hunter
[题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...
- 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(dp)
题意:有30001个岛,在一条线上,从左到右编号一次为0到30000.某些岛屿上有些宝石.初始的时候有个人在岛屿0,他将跳到岛屿d,他跳跃的距离为d.如果当前他跳跃的距离为L,他下一次跳跃的距离只能为 ...
- Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP
题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...
- [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【考虑可用范围】
题目链接:http://codeforces.com/problemset/problem/505/C 题意: 有n个宝石,分别在位置p[i].(1 <= n,p[i] <= 30000) ...
- [Codeforces Round#286] A.Mr. Kitayuta, the Treasure Hunter 【Normal DP..】
题目链接:CF#286 - A 这场CF就这样爆零了...我真是太蒟蒻了... 题目分析 比赛的时候看到A题就发现不会,之后一直也没想出来,于是就弃了,还好不提交也不掉Rating... 比赛后看评论 ...
- 505C Mr. Kitayuta, the Treasure Hunter
传送门 题目大意 一共有30000个位置,从第0个位置开始走,第一次走k步,对于每一次走步,可以走上一次的ki+1 ,ki ,ki-1步数(必须大于等于1),每个岛上有value,求最大能得到的val ...
- cf 506 A. Mr. Kitayuta, the Treasure Hunter
不知道这个sb题怎么做错了.. /*#include <bits/stdc++.h> #define LL long long using namespace std; inline in ...
随机推荐
- android的对话框
android中的对话框形式有四种,分别是一般对话框形式,列表对话框形式,单选按钮对话框,多选按钮对话框,下面我一一对他们进行详解. <一>一般对话框 一般对话框形式如下图: 具体实现代码 ...
- HUNAN 11569 Just Another Knapsack Problem(AC自动机+dp)
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11569&courseid=0 给出目标串,每个子串和 ...
- Idea配置Tomcat以及maven
配置Tamcat Run-->Edit Configurations +-->如果列表里没有tomcat-->点击33 items more irrelevant继续寻找--> ...
- 【编码】封装RedisPubSub工具
基本介绍 核心原理:利用Redis的List列表实现,发布事件对应rpush,订阅事件对应lpop 问题一:Redis不是自带Pub/Sub吗? redis自带的pub/sub有两个问题: 1.如果发 ...
- avi视频文件提取与合并
最近在做一个avi视频文件的提取与合并,花了几天熟悉avi文件格式.制作了一个提取与合并的动态库,不过仅限于提取视频,视频的合并还没添加一些额外判断,可能导致不同分辨率的视频文件合成后不能播放.欢迎大 ...
- 字符串哈希hash
题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转 ...
- Tar压缩文件
[root@test /root]# tar [-zxcvfpP] filename [root@test /root]# tar -N 'yyyy/mm/dd' /path -zcvf targ ...
- 1054. 求平均值 (20)-PAT乙级真题
今天刚刚到学校,2017年学习正式开始了,今天看到了浙大的<数据结构>这学期又要开课了,决定一定要跟着学习一遍:在大学生mooc网上学习:http://www.icourse163.org ...
- ZT:三十个好习惯
- Ubuntu下编译Android JNI实例全过程
第一步:保证make和gcc可用 在shell中输入make-v.不报错就是对的.(可參考http://wenku.baidu.com/view/d87586c24028915f804dc24a.ht ...