[理工] 108交大資演 23小題

作者: jacksoncsie (資工肥宅)   2021-12-25 00:48:55
https://i.imgur.com/3qPTUj3.jpg
https://i.imgur.com/EzJ47yM.jpg
問這題,前情提要答案是 D
而原題答案 X = 91
(4 + 4) + 8 + (5 + 4) + (3 + 5)
= (8 + 8) + (9 + 8) = (16 + 17)
→ 8 + 9 + 8 + 16 + 17 +33 = 91
我是想問演算法實作的部分,
看之前上討論只說可以用 matrix chain 類似方式去求解,
而 matrix chain 最快可在 O(n lg n )下實作
該篇算法
http://i.stanford.edu/pub/cstr/reports/cs/tr/81/875/CS-TR-81-875.pdf
以下是我的想法 :
Find_Small_Intermediate_Sum(int *list, int list_size)
Sequential search list, determine the total size is odd or even
if(list_size % 2 == 0){
Divide the list into group, which group size is 2
Calculate the sum for the each group, put it into the list
Find_Small_Intermediate(list, list_size / 2)
}else{
list \ the biggest one // Don't calculate the biggest one
Divide the list into group, which group size is 2
Calculate the sum for the each group, put it into the list
Find_Small_Intermediate(list, list_size / 2)
}
# 演算法步驟
1. Split the list // O(log n)
2. 判斷總數為何 若為基數 先去除最大項, 偶數不變 // O(n)
3. 接下來依剩下儲存值與相臨數值相加之後 merge // O(n)
有問題歡迎指證 感恩
作者: FRAXIS (喔喔)   2021-12-25 12:46:00
你假設 list size 是 2 的次方數..你的方法會考慮最佳解是先加第二元素和第三個嗎?感覺你的方法不會認定第二個和第三個元素會相鄰
作者: jacksoncsie (資工肥宅)   2021-12-25 13:17:00
F大你好,之前也是看你有回復同樣問題才想上來問的第3項由於是最大值,且總數是基數所以我打算先不加此項,有點像merge sort判斷說如是基數項,則直接mapping到下階段的陣列而關於演算法實作的部分,我是有2個準則https://i.imgur.com/AkO95SC.png我的樹大概畫成這樣https://i.imgur.com/zrLIXTR.jpgroot 是 33 寫錯
作者: mathtsai (mathtsai)   2021-12-26 11:55:00
這題一臉dp(?)
作者: FRAXIS (喔喔)   2021-12-27 11:51:00
這題如果是用 DP, 你可以很容易說明為什麼所有可能都會被考慮到 最基本的形式會是 O(n^3) 時間複雜度如果要加速 你必須要說明你節省的地方是不可能會有最佳解
作者: jacksoncsie (資工肥宅)   2021-12-27 22:21:00
了解 之前看討論也是說用DP 不過我這感覺像Greedy霍夫曼樹的變形 :(

Links booklink

Contact Us: admin [ a t ] ucptt.com