C. Mr. Kitayuta, the Treasure Hunter
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
Input 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.
Output Print the maximum number of gems that Mr. Kitayuta can collect.
Examples input
4 10
10
21
27
27
output
3
input
8 8
9
19
28
36
45
55
66
78
output
6
input
13 7
8
8
9
16
17
17
18
21
23
24
24
26
30
output
4
Note In the first sample, the optimal route is 0 ?→? 10 (+1 gem) ?→? 19 ?→? 27 (+2 gems) ?→?…
In the second sample, the optimal route is 0 ?→? 8 ?→? 15 ?→? 21?→? 28 (+1 gem) ?→? 36 (+1 gem) ?→? 45 (+1 gem) ?→? 55 (+1 gem) ?→? 66 (+1 gem) ?→? 78 (+1 gem) ?→?…
In the third sample, the optimal route is 0 ?→? 7 ?→? 13 ?→? 18 (+1 gem) ?→? 24 (+2 gems) ?→? 30 (+1 gem) ?→?…
题意
有30000个岛屿,某些岛屿上会有宝藏,现在要从岛0开始移动,每次只能往序号更大的岛移动,如果第i-1次移动的距离为l,那么第i次的移动只能有三种情况,l-1、l、l+1(必须要满足都>1),第一次移动的距离为d,问最多能够获得多少宝藏。
思路
很自然的一个状态是 fi,j 表示当前在 i 上一步跳了 j 时的宝藏数 看一下数据范围 都在 3×10^4 数量级 空间炸了 但是题目中给出每次跳的距离与上一次的浮动为 c?1,c,c+1 假设每一步都与上一步发生浮动 一共跳跃了 x 步 有(d + d + x) * x / 2 <= n 设 d=1,n=3×104 算出的 x 大概为三百左右 但是不到 这样我们可以将状态改为fi,j 表示当前在 i 位置 上一步跳了 d+j 时的宝藏数这样空间就没有问题了 但是很明显 j 是有负数的 所以需要全部加上一个常数 调整下标 转移考虑上一次的位置及跳跃距离即可
AC代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int M = 3e4 + 10;
int f[M][510];
int a[N]; //宝藏
int main(){
int n, d;
cin >> n >> d;
for (int i = 1; i <= n;i ++){
int x;cin >> x;
a[x]++;
}
memset(f, -0x3f, sizeof f);
f[d][250] = a[d];
int ans = a[d];
for (int i = d + 1; i <= 30000;i ++){
for (int j = 0; j <= 500;j ++){
int last = i + 250 - (d + j);
if(last < 0 || last >=i) continue;
for (int k = -1; k <= 1;k ++){
if(j + k > 0){
f[i][j]=max(f[i][j],f[last][j+k]+a[i]);
}
}
ans = max(ans, f[i][j]);
}
}
cout << ans << endl;
return 0;
}
|