[Codeforces 505C]Mr. Kitayuta, the Treasure Hunter
Posted NaVi_Awson
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Codeforces 505C]Mr. Kitayuta, the Treasure Hunter相关的知识,希望对你有一定的参考价值。
Description
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.
Sample Input
4 10
10
21
27
27
Sample Output
3
HINT
In the first sample, the optimal route is 0 → 10 (+1 gem) → 19 → 27 (+2 gems) → ...
题解
定义$f[i][j]$为走到$pos$为$i$时前一步步数为$j$的最大收益。
然而空间复杂度$O(n^2)$显然会爆...
后来稍微想想:因为步数每次只会变化$1$,所以我们考虑实际不同的步数远不足$N$,于是准备将步数值$hash$,$j$值就变成了取的$hash$值。
但后来又想:既然每次只会变化$1$,显然这步数是一段连续的区间,我们不需要$hash$这么麻烦,直接把数组整体左移就可以了。
我们假设不同的步数整体数值最小,那么是$1+2+3+…+250>30000$,那么其实我们数组第二维只要开$2*250=500$这么大就可以了。
1 //It is made by Awson on 2017.10.9 2 #include <map> 3 #include <set> 4 #include <cmath> 5 #include <ctime> 6 #include <queue> 7 #include <stack> 8 #include <vector> 9 #include <cstdio> 10 #include <string> 11 #include <cstdlib> 12 #include <cstring> 13 #include <iostream> 14 #include <algorithm> 15 #define query QUERY 16 #define LL long long 17 #define Max(a, b) ((a) > (b) ? (a) : (b)) 18 #define Min(a, b) ((a) < (b) ? (a) : (b)) 19 using namespace std; 20 const int N = 30000; 21 22 int n, d, a[N+5], p; 23 int f[N+5][505], ans; 24 25 void work() { 26 scanf("%d%d", &n, &d); 27 for (int i = 1; i <= n; i++) { 28 scanf("%d", &p); 29 a[p]++; 30 } 31 memset(f, -1, sizeof(f)); 32 f[d][250] = a[d]; 33 for (int i = d; i <= N; i++) 34 for (int j = 1; j <= 500; j++) 35 if (f[i][j] != -1) { 36 ans = Max(ans, f[i][j]); 37 int l = d+j-250; 38 if (l > 1 && i+l-1 <= N) f[i+l-1][j-1] = Max(f[i+l-1][j-1], f[i][j]+a[i+l-1]); 39 if (i+l <= N) f[i+l][j] = Max(f[i+l][j], f[i][j]+a[i+l]); 40 if (i+l+1 <= N) f[i+l+1][j+1] = Max(f[i+l+1][j+1], f[i][j]+a[i+l+1]); 41 } 42 printf("%d\n", ans); 43 } 44 int main() { 45 work(); 46 return 0; 47 }
以上是关于[Codeforces 505C]Mr. Kitayuta, the Treasure Hunter的主要内容,如果未能解决你的问题,请参考以下文章
codeforces 505C - Mr. Kitayuta, the Treasure Hunter(dp)
Codeforces 506E Mr. Kitayuta's Gift - 动态规划 - 矩阵
php dbInsert Helper。 Contoh penggunaan array untuk men-generate INSERT statement sehingga kita tidak
Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph
Codeforces 505E Mr. Kitayuta vs. Bamboos (贪心,二分答案,堆)
Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP