Prob_Solve 板


LINE

看到這個題目,直覺就是coin change的DP解。 提到換零錢應該很多人都可以想出解答了。 的確,這取決於獎金總和開陣列是否記憶體能撐得住。 附上程式碼和點題。 總之,對於[目前金額 - 獎金], 如果[]是有解的,並且加總開獎號碼小於等於5個號碼時, 目前金額的號碼就要累計加總的開獎號碼。 另外,這個程式碼可能開出3個號碼(小於5個)但金額最高, 那就請你任選2個多餘的號碼搭配成5個。 另外2,如果開2個和開5個會是相同的獎金總和,會跑出開5個。 程式碼不到100行,應該不難看懂。 #include <iostream> #include <vector> #include <set> using namespace std; const int BIT = 21; int count_bit(int n) { int ret = 0; n >>= 1; for(int i = 1; i < BIT; ++i, n >>= 1) { ret += n & 1; } return ret; } int main() { int cases; scanf("%d", &cases); while(cases--) { int n; // data size scanf("%d", &n); vector<int> coin(n); vector<int> coin_bit(n, 0); int q; // number size int num; // number int sum = 0; // sum of coin for(int i = 0; i < n; ++i) { scanf("%d%d", &coin[i], &q); sum += coin[i]; for(int j = 0; j < q; ++j) { scanf("%d", &num); coin_bit[i] |= 1 << num; } } vector< set<int> > method(sum + 1); method[0].insert(0); for(int i = 0; i < n; ++i) { for(int j = sum; j >= coin[i]; --j) { int k = j - coin[i]; if(method[k].size()) { set<int> :: iterator it = method[k].begin(); while(it != method[k].end()) { int new_int = *it | coin_bit[i]; if(count_bit(new_int) <= 5) { method[j].insert(new_int); } ++it; } } } } int total = 0, max_bit = 0, max_value = 0; for(int i = sum; i > 0; --i) { int size = method[i].size(); if(size > 0) { total = i; set<int> :: iterator it = method[i].begin(); while(it != method[i].end()) { int j = count_bit(*it); if(max_bit < j) { max_bit = j; max_value = *it; } ++it; } break; } } if(total > 0) { printf("%d, ", total); printf("choose: "); max_value >>= 1; for(int i = 1; i < BIT; ++i, max_value >>= 1) { if(max_value & 1) { printf("%d ", i); } } puts(""); } else { puts("none"); } } return 0; } 有錯跟我說,我再改。 Bleed ※ 引述《shipship (Ship)》之銘言: : 最近在跑一個模擬,遇到一個最佳化問題請各位板大幫忙看看: : 現有一個對獎系統,從20個號碼中選5個做為這次的中獎號碼 : 有一群下注資料,格式如下: : 978 3 2 10 13 //獎金978元,買了三個號碼,分別為2,10,13 : 5921 2 1 14 : 8027 4 1 4 6 9 : 7931 4 5 9 10 15 //獎金7931元,買了四個號碼,分別為5,9,10,15 : 4957 2 2 16 : 中獎的條件是該客人所買的號碼全中(全部都在5個中獎號碼中出現) : 假設今日開獎號碼為1 2 4 10 13 16 : 則總獎金為978+4957 : 請求出,開出哪5個號碼,可以使得大家所得到的獎金最高? : 每個人可以買的號碼數量為2~5,資料筆數不超過六千 : 我想了好久,目前都出的演算法,分析一下都還是暴力解。 : 請板大有甚麼意見請踴躍討論 感謝 --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.43.115.234







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP