2019 GDUT Rating Contest II : Problem G. Snow Boots
生活随笔
收集整理的這篇文章主要介紹了
2019 GDUT Rating Contest II : Problem G. Snow Boots
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題面:
G. Snow Boots
Input ?le: standard input Output ?le: standard output Time limit: 1 second Memory limit: 256 megabytes It’s winter on the farm, and that means snow! There are N tiles on the path from the farmhouse to the barn, conveniently numbered 1...N, and tile i is covered in fi feet of snow. Farmer John starts o? on tile 1 and must reach tile N to wake up the cows. Tile 1 is sheltered by the farmhouse roof, and tile N is sheltered by the barn roof, so neither of these tiles has any snow. But to step on the other tiles, Farmer John needs to wear boots! In his foul-weather backpack, Farmer John has B pairs of boots, numbered 1...B. Some pairs are more heavy-duty than others, and some pairs are more agile than others. In particular, pair i lets FJ step in snow at most si feet deep, and lets FJ move at most di forward in each step. Unfortunately, the boots are packed in such a way that Farmer John can only access the topmost pair at any given time. So at any time, Farmer John can either put on the topmost pair of boots (discarding his old pair) or discard the topmost pair of boots (making a new pair of boots accessible). Farmer John can only change boots while standing on a tile. If that tile has f feet of snow, both the boots he takes o? AND the boots he puts on must be able to withstand at least f feet of snow. Intermediate pairs of boots which he discards without wearing do not need to satisfy this restriction. Help Farmer John minimize waste, by determining the minimum number of pairs of boots he needs to discard in order to reach the barn. You may assume that Farmer John is initially not wearing any boots. Input The ?rst line contains two space-separated integers N and B (2 ≤ N,B ≤ 250). The second line contains N space-separated integers. The ith integer is fi, giving the depth of snow on tile i (0 ≤ fi ≤ 109). It’s guaranteed that f1 = fN = 0. The next B lines contain two space-separated integers each. The ?rst integer on line i + 2 is si, the maximum depth of snow in which pair i can step. The second integer on line i + 2 is di, the maximum step size for pair i. It’s guaranteed that 0 ≤ si ≤ 109 and 1 ≤ di ≤ N ?1. The boots are described in top-to-bottom order, so pair 1 is the topmost pair in FJ’s backpack, and so forth. Output The output should consist of a single integer, giving the minimum number of boots Farmer John needs to discard. It’s guaranteed that it will be possible for FJ to make it to the barn. Example Input 10 4 0 2 8 3 6 7 5 1 4 0 2 3 4 2 3 4 7 1 Output 2題目描述:
農(nóng)夫想從農(nóng)舍到谷倉(去喂奶牛?),去谷倉的路由N個(gè)瓷磚組成,按1-N編號,農(nóng)夫從瓷磚1出發(fā)到瓷磚N,就可以到達(dá)谷倉了。由于下雪,瓷磚被雪不同程度地覆蓋(有的瓷磚上積雪厚一點(diǎn),有的薄一點(diǎn))。因此,農(nóng)夫必須穿靴子才能走到谷倉。在農(nóng)夫的背包里,有B雙靴子,按1-B編號。這些靴子有高的,也有矮的;有的跨一步跨遠(yuǎn)一點(diǎn)的(假如一雙最多能跨x距離,那么,農(nóng)夫可以穿著這雙鞋子跨1距離,2距離...x距離),有的跨一步跨近一點(diǎn)。但是這個(gè)背包有個(gè)特點(diǎn):只能拿背包最上面的靴子(這是什么背包來的,只能拿最上面的靴子? →_→),如果要拿背包下面的靴子,就只能把背包最上面的靴子丟掉,直到想拿到的靴子“浮”現(xiàn)在背包的最上面(有點(diǎn)像棧,可以用棧理解這個(gè)背包)。當(dāng)前站的位置穿的鞋子高度不能低于積雪高度(低于的話靴子會“進(jìn)”雪 ( ⊙ o ⊙ ) )。如果想要換一雙靴子,就必須從背包拿出比當(dāng)前積雪要高的靴子,然后穿上新鞋子,丟掉舊鞋子。求:怎樣走才能使丟棄的鞋子數(shù)量最小,輸出丟棄鞋子最小的數(shù)量題目分析:
前言:這道題我看英文的時(shí)候各種看不懂,樣例都看不懂(太渣了? ( /。\ )? ),到后面又忽略了一些細(xì)節(jié),導(dǎo)致想了很久(?(T_T)?) 這道題是用動態(tài)規(guī)劃去解決的,我們可以先看看樣例是怎樣的(看懂樣例的可以無視我這渣渣解說): 首先,我們是在瓷磚1的位置,也就是: 由題意得知,我們在第1塊瓷磚上是沒有穿靴子的,這時(shí)我們穿上靴子1,背包情況: 然后往前面跨三塊瓷磚,到達(dá)瓷磚4,也就是: 這時(shí)我們發(fā)現(xiàn),這個(gè)靴子無論是跨幾步瓷磚,都不能往前走了,所以我們要拿出新靴子,看能不能繼續(xù)往前走。 這時(shí)我們丟棄了靴子1,穿上了靴子2,發(fā)現(xiàn)最多只能跨兩個(gè)瓷磚,也就是最遠(yuǎn)距離到: 但是還是不行,因?yàn)楹脱プ?的問題一樣,無論到哪個(gè)瓷磚靴子都會進(jìn)“雪”(靴子高度不夠) 所以,我們要再換一雙鞋子,然后丟掉舊鞋子: 之后,我們就可以這樣走: 這樣農(nóng)夫就穿著靴子3成功到達(dá)了終點(diǎn),路上丟棄了兩雙鞋子 當(dāng)然,剛開始的時(shí)候我們在出發(fā)時(shí)就從背包拿出靴子3(首先要丟棄靴子1和靴子2),然后直接穿著這個(gè)靴子到達(dá)終點(diǎn)。 也是只需要丟棄兩雙鞋子。 那這道題怎樣用動態(tài)規(guī)劃呢?一般動態(tài)規(guī)劃的題有這樣的特點(diǎn):有多種途徑求出到“最”值,而且當(dāng)前第n個(gè)狀態(tài)的最值可以由前n-1個(gè)狀態(tài)的最值推算出來。特點(diǎn)1已經(jīng)符合了,那么,我們可能會往動態(tài)規(guī)劃方面去想:這個(gè)題目的問題:到達(dá)第N個(gè)瓷磚所需要丟棄的最少鞋子。子問題:到達(dá)第i個(gè)瓷磚所需要丟棄的最少鞋子(1 <= i < N)。這時(shí)需要考慮:假設(shè)知道了子問題的結(jié)果,怎樣計(jì)算要求的問題。在這里就是:如果我到達(dá)了第i個(gè)瓷磚,怎樣選才能到達(dá)第N個(gè)瓷磚?(我的代碼是按照這個(gè)想法寫的,想法不同代碼也不同,動態(tài)規(guī)劃的題AC代碼不唯一)。在這里我是從前面去“更新”后面的值(即由前面瓷磚的最少丟棄鞋子“更新”到后面瓷磚的最少丟棄鞋子),dp[i]定義為到當(dāng)前第i個(gè)瓷磚要丟棄的最少鞋子。這個(gè)dp[i]有個(gè)特點(diǎn):由dp[i]還可以推測到第i個(gè)瓷磚時(shí)農(nóng)夫正在穿的鞋子,為什么?因?yàn)楸嘲容^特殊,取出來和穿過的鞋子不能再放回背包,所以農(nóng)夫到第i個(gè)瓷磚穿的鞋子就是dp[i]+1,可以用上面解釋樣例的背包圖理解一下這個(gè)。 解決完這些問題,基本上可以寫出代碼出來了,不過有一點(diǎn)要注意的是:從當(dāng)前瓷磚到下一個(gè)瓷磚,如果要換鞋子的話,拿出鞋子的高度就不能低于當(dāng)前瓷磚的雪的高度;如果要到下一個(gè)瓷磚,那么穿的鞋子一定不能低于下一個(gè)瓷磚的高度。有點(diǎn)人可能對dp數(shù)組的初始化有疑問:如果按照上面dp數(shù)組的解說,dp[1] = 0,即第1個(gè)瓷磚正在穿的鞋子是dp[1]+1 = 1,也就是第1雙鞋子,但是題目說在第1個(gè)瓷磚沒有穿鞋子啊。其實(shí)這個(gè)自己仔細(xì)考慮一下,第1個(gè)瓷磚穿與不穿第1雙鞋子是不是沒有影響?為了更方便我們計(jì)算,就假設(shè)在第1個(gè)瓷磚穿著第1雙鞋子,也就是dp[1] = 0. AC代碼: 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #include <set> 6 #include <algorithm> 7 using namespace std; 8 const int inf = 0x3f3f3f3f; 9 int n, b; 10 int f[255], s[255], d[255]; 11 int dp[555]; 12 13 void test(){ 14 cout <<endl; 15 for(int i = 1 ; i <= n ; i++){ 16 printf("dp[%d] : %d\n", i, dp[i]); 17 } 18 } 19 20 int main(){ 21 cin >> n >> b; 22 for(int i = 1; i <= n; i++){ 23 cin >> f[i]; 24 } 25 for(int i = 1; i <= b; i++){ 26 cin >> s[i] >> d[i]; 27 } 28 29 memset(dp, inf, sizeof(dp)); 30 dp[1] = 0; //第1個(gè)瓷磚丟棄0雙鞋子,可以表示當(dāng)前正在穿第1雙鞋子 31 32 33 for(int i = 1; i <= n; i++){ 34 int u = dp[i]+1; //第i個(gè)瓷磚丟器正在穿第u雙鞋子 35 for(int j = u; j <= b; j++){ //穿第j雙鞋子到下一個(gè)瓷磚 36 for(int k = 1; k <= d[j]; k++){ //到第i+k個(gè)瓷磚 37 if(f[i+k] <= s[j] && f[i] <= s[j]){ //穿的鞋子高于等于當(dāng)前瓷磚和下一個(gè)瓷磚 38 dp[i+k] = min(dp[i+k], j-1); //穿第j雙鞋子到第i+k個(gè)瓷磚,丟棄了j-1雙鞋子 39 //min()是更新操作,也就是如果其他瓷磚到達(dá)第i+k個(gè)瓷磚有更小的丟棄數(shù)量就更新為更小的 40 } 41 } 42 } 43 } 44 45 //test(); 46 47 cout << dp[n] << endl; 48 49 return 0; 50 }?
?
轉(zhuǎn)載于:https://www.cnblogs.com/happy-MEdge/p/10405028.html
總結(jié)
以上是生活随笔為你收集整理的2019 GDUT Rating Contest II : Problem G. Snow Boots的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python指定版本 安装模块包
- 下一篇: 【LeetCode】Recursion(