日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【每日一题】8月12日题目精讲 Mr. Kitayuta, the Treasure Hunter

發(fā)布時(shí)間:2023/12/3 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【每日一题】8月12日题目精讲 Mr. Kitayuta, the Treasure Hunter 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

來(lái)源:牛客網(wǎng):

時(shí)間限制:C/C++ 1秒,其他語(yǔ)言2秒 空間限制:C/C++ 262144K,其他語(yǔ)言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
輸入
復(fù)制

4 10 10 21 27 27

輸出
復(fù)制

3

示例2
輸入
復(fù)制

8 8 9 19 28 36 45 55 66 78

輸出
復(fù)制

6

示例3
輸入
復(fù)制

13 7 8 8 9 16 17 17 18 21 23 24 24 26 30

輸出
復(fù)制

4

備注:

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) ?→?…

題意:

一維坐標(biāo)共有30001個(gè),坐標(biāo)從0~30000,其中有n個(gè)坐標(biāo)有寶座,起始點(diǎn)為0,給出第一步跳躍距離d,之后每一步可以從上一步x的基礎(chǔ)上選擇x,x-1,x+1。
問(wèn)最多能拿多少寶藏?

題解:

第一反應(yīng)是dp
dp[i][j]表示到達(dá)第i個(gè)島嶼,跳的距離為j得到的最大寶藏?cái)?shù)
從上一次狀態(tài)之后,可以跳三種情況,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])
我們將上面三個(gè)式子可以整合在一起:
dp[i][j]=max(dp[i][j],dp[next][j+k]+gem[i])
k的范圍是-1~1
next=i -( j + d - 250 )

第一維數(shù)組開30000
第二維不能再開這么大,
我們算一算最大步長(zhǎng)為1+2+3+…a+250 > 30000,所以最大也就是從第一次d的基礎(chǔ)上上下浮動(dòng)250,所以開500+夠用
我們規(guī)定 j=250 時(shí)表示和d相等,每次可以跳d,d-1,d+1,而下下次又是再上次基礎(chǔ)上加減1和不變,所以( j -250 )+d表示當(dāng)前跳躍的距離,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]++;//記錄寶藏坐標(biāo)數(shù)量 } memset(dp,-1,sizeof(dp));dp[d][250]=gem[d];//相當(dāng)于把250當(dāng)做0步//每次移動(dòng)就是250上下浮動(dòng) for(int i=d;i<=30000;i++)//i表示當(dāng)前距離 {for(int j=1;j<=500;j++){int jump=(j+d-250);//表示跳的距離 int next=i-jump;//表示上一次的位置 if(next<0||next>=i)continue;//不往回跳 //上一次的位置一定在當(dāng)前位置之前,否則成了向后跳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]);//轉(zhuǎn)移方程 }sum=max(sum,dp[i][j]);}}cout<<sum<<endl;return 0; }

總結(jié)

以上是生活随笔為你收集整理的【每日一题】8月12日题目精讲 Mr. Kitayuta, the Treasure Hunter的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。