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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

topcoder-SRM565-div2-第二题-500分--搜索/动态规划

發布時間:2025/5/22 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 topcoder-SRM565-div2-第二题-500分--搜索/动态规划 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

不想看英文的題目?翻譯在后面

Problem Statement

???? Manao is traversing a valley inhabited by monsters. During his journey, he will encounter several monsters one by one. The scariness of each monster is a positive integer. Some monsters may be scarier than others. The i-th (0-based index) monster Manao will meet has scariness equal to dread[i].

Manao is not going to fight the monsters. Instead, he will bribe some of them and make them join him. To bribe the i-th monster, Manao needs price[i] gold coins. The monsters are not too greedy, therefore each value in price will be either 1 or 2.

At the beginning, Manao travels alone. Each time he meets a monster, he first has the option to bribe it, and then the monster may decide to attack him. A monster will attack Manao if and only if he did not bribe it and its scariness is strictly greater than the total scariness of all monsters in Manao's party. In other words, whenever Manao encounters a monster that would attack him, he has to bribe it. If he encounters a monster that would not attack him, he may either bribe it, or simply walk past the monster.



Consider this example: Manao is traversing the valley inhabited by the Dragon, the Hydra and the Killer Rabbit. When he encounters the Dragon, he has no choice but to bribe him, spending 1 gold coin (in each test case, Manao has to bribe the first monster he meets, because when he travels alone, the total scariness of monsters in his party is zero). When they come by the Hydra, Manao can either pass or bribe her. In the end, he needs to get past the Killer Rabbit. If Manao bribed the Hydra, the total scariness of his party exceeds the Rabbit's, so they will pass. Otherwise, the Rabbit has to be bribed for two gold coins. Therefore, the optimal choice is to bribe the Hydra and then to walk past the Killer Rabbit. The total cost of getting through the valley this way is 2 gold coins.

You are given the vector <int>s dread and price. Compute the minimum price Manao will pay to safely pass the valley.

Definition

????
Class: MonstersValley2
Method: minimumPrice
Parameters: vector <int>, vector <int>
Returns: int
Method signature: int minimumPrice(vector <int> dread, vector <int> price)
(be sure your method is public)
????
?

Constraints

- dread will contain between 1 and 20 elements, inclusive.
- Each element of dread will be between 1 and 2,000,000,000, inclusive.
- price will contain between the same number of elements as dread.
- Each element of price will be either 1 or 2.

Examples

0) ?
????
{8, 5, 10}
{1, 1, 2}
Returns: 2
The example from the problem statement.
1)?
????
{1, 2, 4, 1000000000}
{1, 1, 1, 2}
Returns: 5
Manao has to bribe all monsters in the valley.
2)?
????
{200, 107, 105, 206, 307, 400}
{1, 2, 1, 1, 1, 2}
Returns: 2
Manao can bribe monsters 0 and 3.
3)?
????
{5216, 12512, 613, 1256, 66, 17202, 30000, 23512, 2125, 33333}
{2, 2, 1, 1, 1, 1, 2, 1, 2, 1}
Returns: 5
Bribing monsters 0, 1 and 5 is sufficient to pass safely.

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. ? ??

題目翻譯:

?Manao要走過一片樹林,樹林有怪獸,每個怪獸有一個正整數值的戰斗力,和一個價格(1或2),Manao可以選擇喂養怪獸(花費1或2個金幣),或者直接走過去。Manao也有一個戰斗力值——是他所喂養的怪獸的全體的戰斗力之和。Manao遇到的第i個怪獸戰斗力為dread[i],價格為price[i],如果dread[i]大于Manao當前的戰斗力,Manao就必須喂養它,否則怪獸就會攻擊他,喂養消耗的金幣數為price[i](補充說明:初始Manao戰斗力為0,所以必須喂養第一個怪獸)

求Manao穿過樹林消耗的最少金幣數

輸入限制:

怪獸個數在1~20之間(包含1和20)

怪獸戰斗力在1~2×10^9之間

price的值為1或2

price和dread的包含元素個數相同

題目解析:

?考慮遞歸處理,設當前Manao戰斗力為sum,遇到第idx個怪獸(從0開始編號),設從這個時候開始他最少要消耗f(sum,idx)個金幣才能通過樹林

如果sum>=dread[idx],有兩種選擇,喂養或者直接走過去。如果喂養,付出price[idx]個金幣,同時戰斗力增加到sum+dread[idx],然后就可以處理下一個怪獸了,這種選擇對應的最少消耗金幣數為price[idx] + f(sum + dread[idx], idx + 1);如果不喂養,那么直接處理下一個怪獸,這種選擇對應的最少消耗金幣數為f(sum, idx + 1)。我們只有這兩種選擇,所以f(sum,idx)=min(price[idx] + f(sum + dread[idx], idx + 1),?f(sum, idx + 1))

如果sum<dread[idx], 只有喂養,f(sum, idx) =?price[idx] + f(sum + dread[idx], idx + 1)

遞歸的邊界是:如果idx >= 怪獸個數,f(sum, idx) = 0

(寫著寫著就感覺有點像算法導論動態規劃那一章的裝配線問題)

下面是代碼:

純粹的遞歸搜索:

(這里有個嚴重的效率問題,即宏中使用了函數,應該把define那行注釋掉,具體見http://www.cnblogs.com/fstang/archive/2013/01/07/2849317.html?---補充于2013-01-07)

#include <string> #include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std; #define min(a,b) ((a) < (b) ? (a) : (b))class MonstersValley2{ public:int minimumPrice(vector <int> dread, vector <int> price){return f(0, 0, dread, price);}int f(long long sum, int idx, vector <int> &dread, vector <int> &price){if (idx >= dread.size()) {return 0;}if (sum >= dread[idx]) {int m = min(price[idx] + f(sum + dread[idx], idx + 1, dread, price), f(sum, idx + 1, dread, price)); return m;} else {int m = price[idx] + f(sum + dread[idx], idx + 1, dread, price);return m; }} };
//這個代碼有一個超過200ms,有一個超時,其他為0ms或1ms...不能通過

?

帶備忘錄的遞歸搜索(避免重復計算)

(這里同樣有嚴重的效率問題,即宏中使用了函數,應該把define那行注釋掉,具體見http://www.cnblogs.com/fstang/archive/2013/01/07/2849317.html?---補充于2013-01-07)

#include <string> #include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std; #define min(a,b) ((a) < (b) ? (a) : (b)) class Elem{ public:long long sum;int idx;Elem(long long s, int i){sum = s;idx = i;}bool operator < (const Elem &rhs) const {return sum < rhs.sum || sum == rhs.sum && idx < rhs.idx;} };class MonstersValley2{ public:map<Elem, int> S;int minimumPrice(vector <int> dread, vector <int> price){S.clear();return f(0, 0, dread, price);}int f(long long sum, int idx, vector <int> &dread, vector <int> &price){map<Elem, int>::iterator it = S.find(Elem(sum, idx));if (it != S.end()) {return it->second;}if (idx >= dread.size()) {return 0;}if (sum >= dread[idx]) {int m = min(price[idx] + f(sum + dread[idx], idx + 1, dread, price), f(sum, idx + 1, dread, price));S.insert(make_pair(Elem(sum, idx), m)); return m;} else {int m = price[idx] + f(sum + dread[idx], idx + 1, dread, price);S.insert(make_pair(Elem(sum, idx), m));return m; }} };
//有5個案例運行時間超過200ms,最長480ms,無超時,多數為0ms或1ms,通過

?寫了個main函數測試一下(要修改類代碼,增加一個cnt成員變量(public),每次從表中查到就cnt++),這個案例就是之前超時的那個,有19個元素,得到結果是:

最小總花費為2,從表中查到的總次數cnt=131071,可見還是有一些重疊子問題的,雖然我感覺sum是很稀疏的,重疊的可能性不大

int main(int argc, char* argv[]) {int a[] = {1782688262, 895047095, 1625373870, 1009836949, 985560038, 1470346827, 296839142, 34727454, 413009041, 1114435639, 1692481802, 422406335, 795130000, 1455087504, 410389760, 961349143, 1693064512, 621415696, 98442513};int b[] = {2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1};MonstersValley2 m;cout << m.minimumPrice(vector<int>(a, a+sizeof(a)/sizeof(int)), vector<int>(b, b+sizeof(b)/sizeof(int))) << endl;cout << "cnt:" << m.cnt << endl;system("pause");return 0; }

更細致的測試:統計每個遞歸層次的查表次數,結果如下:

cnt[0]:0
cnt[1]:0
cnt[2]:1
cnt[3]:2
cnt[4]:4
cnt[5]:8
cnt[6]:16
cnt[7]:32
cnt[8]:64
cnt[9]:128
cnt[10]:256
cnt[11]:512
cnt[12]:1024
cnt[13]:2048
cnt[14]:4096
cnt[15]:8192
cnt[16]:16384
cnt[17]:32768
cnt[18]:65536
cnt[19]:0

?竟然都是2的冪,看到這里我也吃了一驚,為什么是這樣?我再想想……

-----------------------------------------------------the following comments are added on 2013-01-04----------------------------------------------------

看到查表時不同元素的查找頻率是不同的,由于map是用紅黑樹實現的,屬于平衡二叉樹,并不是最優的。有個叫最優平衡二叉樹的算法,當每個元素查找頻率不完全相同時,可以構造相應的二叉查找樹,使得總的代價最小。只是想想,因為這個實現起來就復雜了,還要自己寫二叉查找樹……

?

?===============================================補充=============================================

?補充一個Java版本的,因為考慮到hashmap從查找的效率來講比紅黑樹實現的map更好,但是C++11中才提供unordered_map,所以就試試Java,遺憾的是,這個也不夠好,有個case還是fail了,超時,在我的電腦上跑是out of memory....

還是貼一下,main里就是那個fail的case

(感嘆自己對Java很是不熟,寫代碼到處查,各種錯誤,感覺很不好,關于new的用法還老是和C++扯不清)

import java.util.HashMap;public class MonstersValley {private HashMap<Elem, Integer> map = new HashMap<Elem, Integer>();public int minimumPrice(long[] dread, int[] price){return f(0, 0, dread, price);//return 0; }int f(int idx, long sum, long[] dread, int[] price){Integer res;if (idx >= dread.length) {//遞歸邊界return 0;}if (map.containsKey(new Elem(sum, idx))) {//查表return map.get(new Elem(sum, idx));}if (sum >= dread[idx]) {res = Math.min(f(idx + 1, sum, dread, price), price[idx] + f(idx + 1, sum + dread[idx], dread, price)); } else {res = price[idx] + f(idx + 1, sum + dread[idx], dread, price);}map.put(new Elem(sum, idx), res);return res;}public static void main(String args[]){long dread[] = {8589934592l, 4294967296l, 2147483648l, 1073741824, 536870912, 268435456, 134217728, 67108864, 33554432, 16777216, 8388608, 4194304, 2097152, 1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1, 170191697543L, 442151420049l, 760562175049l, 13784873423l, 178349965388l, 755005234214l, 541764950940l, 946482302586l, 543713207966l, 762391189316l, 871034340034l, 677925419895l, 160143387182l, 105796128936l, 529757177900l, 289213265278l};int price[] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1};MonstersValley m = new MonstersValley();System.out.println(m.minimumPrice(dread, price));} } class Elem{public long sum;public int idx;public Elem(long s, int i){sum = s; idx = i;} }

?

轉載于:https://www.cnblogs.com/fstang/archive/2012/12/23/2829946.html

總結

以上是生活随笔為你收集整理的topcoder-SRM565-div2-第二题-500分--搜索/动态规划的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 在线亚洲观看 | 欧美一级淫片 | www.av72| 天堂а√在线最新版中文在线 | 成人作爱视频 | 亚洲国产精品久久人人爱 | www.好了av.com | 大学生一级一片全黄 | 97久久人澡人人添人人爽 | 日韩欧美小视频 | 少妇高潮久久久久久潘金莲 | 亚洲一区二区三区在线看 | 欧美影院| 91网页版| 亚洲伊人精品 | 国产一区二区波多野结衣 | 成人av在线网址 | 激情网站视频 | 男女涩涩视频 | 992tv在线成人免费观看 | 色乱码一区二区三区网站 | 久久精品aaaaaa毛片 | 97精品在线视频 | 国产露脸无套对白在线播放 | 琪琪电影午夜理论片八戒八戒 | www.youjizz.com在线观看 | 亚洲欧洲在线播放 | 国产精品传媒在线观看 | 粉嫩av一区二区白浆 | 国产又黄又粗又硬 | 日韩毛片大全 | 综合视频在线观看 | 永久免费毛片 | 亚洲色成人网站www永久四虎 | 丁香婷婷一区二区三区 | 97视频在线看 | 大尺度做爰无遮挡露器官 | 日本a视频在线观看 | 午夜性福利 | 28一20岁女人一级 | 婷婷亚洲天堂 | av在线.com| 日韩在线一区视频 | 日韩毛片高清在线播放 | 国产欧美三级 | 人妻色综合网站 | 久久国产亚洲精品无码 | 亚洲a成人 | 成人av中文解说水果派 | 亚洲欧美另类图片 | 一区二区自拍 | 久久久久久999| 懂色av一区二区三区在线播放 | 国产少妇在线 | 亚洲国产精品久久精品怡红院 | 在线网站av | 麻豆视频精品 | 国产一区二区三区电影在线观看 | 岛国a视频| 黑人玩弄人妻一区二区三区影院 | 日韩一二三四五区 | 毛片内射久久久一区 | 91丨porny丨中文 | 国产偷国产偷av亚洲清高 | 日本韩国在线观看 | 亚洲一区二区三区激情 | 老熟妇一区二区三区 | 欧美精品一区二区三区久久久竹菊 | 欧美性猛交bbbbb精品 | 婷婷五月情 | 天天做天天爽 | 欧美日韩在线综合 | 中文字幕欧美一区 | 国产美女又黄又爽又色视频免费 | 中文字幕第一区综合 | 欧美丰满一区二区免费视频 | 95看片淫黄大片一级 | 羞羞的视频在线观看 | 97精品人妻一区二区三区在线 | 国产另类专区 | 激情小说欧美色图 | 福利视频午夜 | 伊人黄| 精品国产乱 | 国产免费不卡视频 | 蜜乳av中文字幕 | 国产寡妇亲子伦一区二区三区四区 | 久久免费看少妇高潮 | 艹少妇视频 | 青青草一区二区三区 | 欧美精品成人一区二区在线观看 | 日日日操操操 | 欧美操操| 五月婷婷综合在线 | 欧美操穴| 亚洲天堂免费在线 | 久久网站视频 | 无码人妻丰满熟妇区毛片蜜桃精品 | 欧美视频在线观看 |