【每日一题】8月12日题目精讲 Mr. Kitayuta, the Treasure Hunter
來源:牛客網:
時間限制:C/C++ 1秒,其他語言2秒 空間限制:C/C++ 262144K,其他語言524288K 64bit IO Format: %lld題目描述
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.
示例1
輸入
復制
輸出
復制
示例2
輸入
復制
輸出
復制
示例3
輸入
復制
輸出
復制
備注:
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) ?→?…
題意:
一維坐標共有30001個,坐標從0~30000,其中有n個坐標有寶座,起始點為0,給出第一步跳躍距離d,之后每一步可以從上一步x的基礎上選擇x,x-1,x+1。
問最多能拿多少寶藏?
題解:
第一反應是dp
dp[i][j]表示到達第i個島嶼,跳的距離為j得到的最大寶藏數
從上一次狀態之后,可以跳三種情況,j,j+1,j-1,由此可得
dp[i+j][j]=max(dp[i+j][j],dp[i][j]+c[i+j])
dp[i+j+1][j+1]=max(dp[i+j+1][j+1],dp[i][j]+c[i+j+1])
dp[i+j-1][j-1] = max(dp[i+j-1][j-1], dp[i][j] + c[i+j-1])
我們將上面三個式子可以整合在一起:
dp[i][j]=max(dp[i][j],dp[next][j+k]+gem[i])
k的范圍是-1~1
next=i -( j + d - 250 )
第一維數組開30000
第二維不能再開這么大,
我們算一算最大步長為1+2+3+…a+250 > 30000,所以最大也就是從第一次d的基礎上上下浮動250,所以開500+夠用
我們規定 j=250 時表示和d相等,每次可以跳d,d-1,d+1,而下下次又是再上次基礎上加減1和不變,所以( j -250 )+d表示當前跳躍的距離,i-(j-250)-d表示上一次的位置
代碼:
#include<bits/stdc++.h> using namespace std; const int maxn=30013; int gem[maxn]; int dp[maxn][520]; int main() {int n,d;cin>>n>>d;int sum=0;for(int i=1;i<=n;i++){int x;cin>>x;gem[x]++;//記錄寶藏坐標數量 } memset(dp,-1,sizeof(dp));dp[d][250]=gem[d];//相當于把250當做0步//每次移動就是250上下浮動 for(int i=d;i<=30000;i++)//i表示當前距離 {for(int j=1;j<=500;j++){int jump=(j+d-250);//表示跳的距離 int next=i-jump;//表示上一次的位置 if(next<0||next>=i)continue;//不往回跳 //上一次的位置一定在當前位置之前,否則成了向后跳for(int k=-1;k<=1;k++){if(j+k>0&&dp[next][j+k]!=-1)dp[i][j]=max(dp[i][j],dp[next][j+k]+gem[i]);//轉移方程 }sum=max(sum,dp[i][j]);}}cout<<sum<<endl;return 0; }總結
以上是生活随笔為你收集整理的【每日一题】8月12日题目精讲 Mr. Kitayuta, the Treasure Hunter的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python expect模块_Pyth
- 下一篇: 【每日一题】8月14日题目精讲 [SC