Codefores 506A Mr. Kitayuta, the Treasure Hunter( DP && dfs )
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.
这个题一开始就想到DP了 。dp[i][j]表示跳到了下标 i 上次跳了 j 个长度的最大得分。
但是d的范围有点大 , 我试了一下开二维记忆化搜索死机了。
后来大洲爷说了一下思路 , d <= 2000 dp , 否则 dfs , d > 2000的话层数应该10来层就OK了吧 。
其几天考试没时间写,刚考完才有时间补一下题来着。 一写就过了~
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
using namespace std;
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lr rt<<1
#define rr rt<<1|1
typedef long long LL;
const int oo = 1e9+;
const double PI = acos(-1.0);
const double eps = 1e- ;
const int N = ;
const int mod = ;
int n , d , x , dp[N][] , cnt[N]; int dfs( int i , int l ) {
if( i > x ) return ;
int tmp = cnt[i];
if( l > ) tmp = max( tmp , cnt[i] + dfs( i + l - , l - ) );
tmp = max( tmp , cnt[i] + dfs( i + l , l ) );
tmp = max( tmp , cnt[i] + dfs( i + l + , l + ) );
return tmp ;
} int DP( int i , int l ) {
if( i > x ) return ;
if( dp[i][l] == - ) {
int &tmp = dp[i][l] ; tmp = cnt[i];
if( l > ) tmp = max( tmp , cnt[i] + DP( i + l - , l - ) );
tmp = max( tmp , cnt[i] + DP( i + l , l ) );
tmp = max( tmp , cnt[i] + DP( i + l + , l + ) );
}
return dp[i][l];
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif // LOCAL
ios::sync_with_stdio(false);
while( cin >> n >> d ) {
memset( cnt , , sizeof cnt ) ;
memset( dp , - , sizeof dp ) ;
for( int i = ; i < n ; ++i ) {
cin >> x ; cnt[x]++;
}
if( d > ) cout << dfs( d , d ) << endl ;
else cout << DP(d,d) << endl ;
}
}
Codefores 506A Mr. Kitayuta, the Treasure Hunter( DP && dfs )的更多相关文章
- 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 ...
- 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(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 Round#286] A.Mr. Kitayuta, the Treasure Hunter 【Normal DP..】
题目链接:CF#286 - A 这场CF就这样爆零了...我真是太蒟蒻了... 题目分析 比赛的时候看到A题就发现不会,之后一直也没想出来,于是就弃了,还好不提交也不掉Rating... 比赛后看评论 ...
- 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】Mr.Kitayuta,the Treasure Hunter
[题目链接]:http://codeforces.com/problemset/problem/505/C [题意] 一开始你跳一步长度为d; 之后你每步能跳d-1,d,d+1这3种步数; 然后在路上 ...
- 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 ...
随机推荐
- oracle至sqlplus的时候出现错误
那个啥,没记录到. 大概这么回事,上去的时候sqlplus不出命令. 然后source一下,出现了sqlplus. 但是呢,sqlplus / as sysdba的时候出现http——proxy ...
- Git--01 基础 - 远程仓库的使用
目录 Git 基础 - 远程仓库的使用 远程仓库的使用 查看远程仓库 添加远程仓库 从远程仓库中抓取与拉取 推送到远程仓库 查看某个远程仓库 远程仓库的移除与重命名 Git 基础 - 远程仓库的使用 ...
- java ArrayList练习题
package java06; /* *随机产生6的1——33的数字,并存储到列表中,再进行遍历 * */ import java.util.ArrayList; import java.util.R ...
- Echarts 自定义数据视图
toolbox : { show : true, feature : { dataView : { optionToContent : function(option) { // 行名称 var ax ...
- SpringIntegration---MongDB
1.依赖 <dependency> <groupId>org.springframework.integration</groupId> <artifactI ...
- webpack CSS处理loader
loader概念: 首先来介绍一下loader,之前我们用webpack来处理我们写的js代码,并且webpack会自动处理js之间相关的依赖.但是,在开发中我们不仅仅有基本的js代码处理,我们也需要 ...
- Java Web学习总结(11)JDBC
一,简介 JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的 ...
- 攻防世界 WEB篇
0x01 ics-06 查看源码发现:index.php 一开始直接用sqlmap跑了下没有发现注入,然后用brupsuite爆破参数 0x02 NewsCenter SQL注入中的POST注入,查阅 ...
- cordova打包apk流程
一.打包 条件: 1.java-jdk 2.Android-sdk ( 安装教程:https://blog.csdn.net/qq_36577136/article/details/80632674 ...
- PPT技巧
1.秋叶个人的PPT三分钟教程 http://www.pptfans.cn/315656.html 2.<说服力-让你的PPT会说话>秋叶 3.<三体> https://w ...