115 年 國立成功大學資訊工程系碩士班《程式設計》

📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊
📄 以下 2 題共用同一段題幹

A campus system stores device IDs. Each ID is a 6-bit binary string. The seven 6-bit keys are 000001, 000010, 000011, 001111, 100000, 101011, and 111110.

第 1 題3 分

Insert the seven keys into an empty digital search tree in the order given above. When searching for key 001111, list the keys visited on the search path, in order.

Answer format: key1 → key2 → …

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

數位搜尋樹(digital search tree)依照鍵值的位元逐層決定方向:根節點使用第 1 位,下一層使用第 2 位,依此類推。位元為 00 往左子樹,位元為 11 往右子樹。

插入時,從根節點開始,依照待插入鍵值各層對應的位元往下走,直到遇到空位置,便將鍵值放入該位置。搜尋時使用相同規則選擇路徑。

解題方法

鍵值依題目順序插入。第一個鍵值 000001 成為根節點。

依序插入其餘鍵值時,相關位置如下:

鍵值插入方向(由根往下)
000010左
000011左 → 左
001111左 → 左 → 右
100000右
101011右 → 左
111110右 → 右
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2 題4 分

Build the compressed binary trie for the same seven 6-bit keys. For each branch node, define its bitNumber as the bit index used at that node to choose the left or right child. The leftmost bit is index 1 and the rightmost bit is index 6. Perform BFS starting from the root. When visiting a branch node, enqueue the left child before the right child. List the bitNumber of each visited branch node in BFS order.

Answer format: bitNum1, bitNum2, …

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

壓縮二元 Trie(compressed binary trie)只在「能把目前鍵值集合分成 0、1 兩組」的位元建立分支節點;所有鍵值在該位元都相同的部分會被略過。bitNumber 依題目規定從最左邊的位元開始編號,範圍為 1 到 6。

廣度優先搜尋(BFS)由根節點開始,逐層訪問分支節點;每次依序將左子節點、右子節點加入佇列。

解題方法

先找出每一組鍵值中,最左側會產生分流的位元:

  • 全部七個鍵值在第 1 位分成 0 與 1,所以根節點的 bitNumber 是 1。
  • 根節點左側的四個鍵值在第 1、2 位相同;第 3 位分成前三個鍵值與 001111,所以分支位元是 3。
  • 000001、000010、000011 這三個鍵值在第 5 位分流;其中 000010 與 000011 再於第 6 位分流。
  • 根節點右側的三個鍵值在第 2 位分成 100000、101011 與 111110;前兩個鍵值再於第 3 位分流。

因此,依層次排列的分支節點如下:

BFS 層次由左至右的分支節點 bitNumber
第 1 層1
第 2 層3、2
第 3 層5、3
第 4 層6

依 BFS 順序串接,得到 1, 3, 2, 5, 3, 6。

建構與走訪程式碼

以下程式碼以遞迴方式找出目前鍵值集合最左側的分流位元,再以佇列依左、右順序走訪分支節點:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3 題3 分

A binary tree stores distinct keys. Based on the following postorder and inorder sequences, list the keys of nodes at level 3 of the tree from left to right. The root is at level 1.

  • Postorder sequence: A B C K D I G J H F E
  • Inorder sequence: A C B D K E I G F H J

Answer format: key1, key2, …

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

重建二元樹時:

  • 後序走訪順序為「左子樹 → 右子樹 → 根節點」,因此序列最後一個鍵值是目前子樹的根。
  • 中序走訪順序為「左子樹 → 根節點 → 右子樹」,因此找到根節點後,根左側與右側的鍵值分別屬於左、右子樹。

題目中的鍵值互異,所以每個根節點都能在中序序列中唯一定位。

解題方法

後序序列的最後一個鍵值是根節點 EE。在中序序列中,以 EE 分開左右子樹:

  • 左子樹中序:A,C,B,D,KA, C, B, D, K
  • 右子樹中序:I,G,F,H,JI, G, F, H, J

左右子樹各有五個節點,因此對應的後序序列分別是 A,B,C,K,DA, B, C, K, D 與 I,G,J,H,FI, G, J, H, F。

左子樹後序序列的最後一個鍵值是 DD。在中序序列 A,C,B,D,KA, C, B, D, K 中,DD 左側為 A,C,BA, C, B,右側為 KK。其中,A,C,BA, C, B 的後序序列是 A,B,CA, B, C,所以根節點是 CC;其左右子節點分別是 AA、BB。

右子樹後序序列的最後一個鍵值是 FF。在中序序列 I,G,F,H,JI, G, F, H, J 中,FF 左側為 I,GI, G,右側為 H,JH, J。左側子樹的根是 GG,且 II 是 GG 的左子節點;右側子樹的根是 HH,且 JJ 是 HH 的右子節點。

因此,重建後的樹為:

          E
        /   \
       D     F
      / \   / \
     C   K G   H
    / \   /     \
   A   B I       J

根節點 EE 位於第 1 層。第 2 層是 D,FD, F;依此往下,第 3 層由左至右是 C,K,G,HC, K, G, H。

關鍵程式碼

以下程式先依後序與中序重建樹,再以佇列依層走訪:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4 題4 分

For a tree with n=15n=15 nodes, what is the maximum possible height of an AVL tree, and what is the maximum possible height of a red-black tree? The root is at level 1.

Answer format: AVL max possible height = ?, Red-black tree max possible height = ?

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

樹根在第 1 層,因此「高度」是根到最深節點所經過的節點層數。

  • AVL 樹:每個節點左右子樹的高度差至多為 1。要讓節點數盡量少、同時高度盡量大,每個節點的兩個子樹高度應相差 1。
  • 紅黑樹:從任一節點到其所有 NIL 葉節點的路徑,黑色節點數相同;紅色節點不能相鄰。最長路徑上的紅黑節點最多交錯排列。

解題方法

AVL 樹

令 N(h)N(h) 表示高度為 hh 的 AVL 樹所需的最少節點數。取 N(0)=0N(0)=0、N(1)=1N(1)=1。高度為 hh 時,最省節點的配置是讓兩棵子樹高度分別為 h−1h-1 與 h−2h-2,所以:

N(h)=1+N(h−1)+N(h−2)N(h)=1+N(h-1)+N(h-2)

依序計算:

N(1)=1,N(2)=2,N(3)=4,N(4)=7,N(5)=12,N(6)=20N(1)=1,\quad N(2)=2,\quad N(3)=4,\quad N(4)=7,\quad N(5)=12,\quad N(6)=20

因為 N(5)=12≤15N(5)=12\le 15,而 N(6)=20>15N(6)=20>15,所以 15 個節點的 AVL 樹高度最多為 5。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 4 題共用同一段題幹

A firewall uses a fast pre-check before running an exact check. All input integers are non-negative. Answer Questions 1–4 based on the program below.

static inline uint32_t f1(int x) { return (uint32_t)(x % 8); }
static inline uint32_t f2(int x) { return (uint32_t)((x / 2) % 8); }
static inline void set1(uint32_t *b, uint32_t p) { *b |= (1u << p); }
static inline int get1(uint32_t b, uint32_t p) { return (b >> p) & 1u; }
static int precheck(uint32_t bits, int x) {
return get1(bits, f1(x)) && get1(bits, f2(x));
}
static int exact_check(int x) {
return (x == 10) || (x == 22);
}
int main(void) {
int base[] = {10, 22};
uint32_t bits = 0;
for (int i = 0; i < (int)(sizeof(base)/sizeof(base[0])); i++) {
set1(&bits, f1(base[i]));
set1(&bits, f2(base[i]));
}
int n;
scanf("%d", &n);
int exact_checks = 0, blocked = 0;
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
if (precheck(bits, x)) {
exact_checks++;
if (exact_check(x)) blocked++;
}
}
printf("%d %d\n", exact_checks, blocked);
return 0;
}

第 1 題3 分

What is precheck(bits, x) mainly used for?

(A) It is an exact membership test because checking two bit positions avoids collisions.
(B) It is a probabilistic membership test that may have false positives.
(C) It is a bitmask-based two-condition rule: it passes a value if either of the tested bit positions is 1.
(D) It is a compact counter test: the function treats the two bit checks as a “count” and passes x only if the count is at least 2.
(E) It is a direct index check: f1(x) and f2(x) map each value to two unique bit positions.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這段 precheck 是類似 Bloom filter(布隆過濾器) 的快速預檢。程式先把已知值 10、22 經由兩個函數映射到位元位置,並將對應位置設為 1;查詢時,只要兩個映射位置都為 1,就通過預檢,才進一步執行 exact_check。

布隆過濾器式的預檢可能發生假陽性:不在集合中的值,也可能因為映射到已被設為 1 的位置而通過。通過預檢不代表該值一定等於 10 或 22。

解題方法

先計算 base 中兩個值對應的位元位置:

值f1(x)=x mod 8f1(x)=x\bmod 8f2(x)=⌊x/2⌋ mod 8f2(x)=\lfloor x/2\rfloor\bmod 8
10102255
22226633

因此,位元位置 2,3,5,62, 3, 5, 6 會被設為 1。查詢時,precheck 要求 f1(x) 和 f2(x) 對應的位置同時為 1,也就是以邏輯 AND 串接兩個檢查。

例如,x=11x=11 時,f1(11)=3f1(11)=3、f2(11)=5f2(11)=5,兩個位置都已設為 1,所以預檢會通過;但 exact_check(11) 為假。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2 題3 分

What is the program output for the following input?

Input:
5
10 14 22 6 30

(A) 2 2
(B) 2 1
(C) 3 2
(D) 3 1
(E) None of the above

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

程式先用位元集合做快速預檢,再對通過預檢的輸入執行精確檢查:

  • f1(x) = x % 8
  • f2(x) = (x / 2) % 8,此處 x 為非負整數,整數除法會捨去小數部分。
  • precheck 只有在位元位置 f1(x) 和 f2(x) 都已設為 1 時才通過。
  • exact_checks 計算通過預檢的輸入數;blocked 則計算其中等於 10 或 22 的輸入數。

解題方法

先依序算出基準值 {10, 22} 設定的位元位置:

基準值f1(x)f2(x)
1010 mod 8=210 \bmod 8 = 2(10/2) mod 8=5(10 / 2) \bmod 8 = 5
2222 mod 8=622 \bmod 8 = 6(22/2) mod 8=3(22 / 2) \bmod 8 = 3

因此,位元集合中設為 1 的位置是 {2,3,5,6}\{2, 3, 5, 6\}。

接著逐一計算輸入:

輸入 xf1(x)f2(x)預檢結果精確檢查結果
1025通過等於 10,封鎖
1467不通過不執行
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3 題3 分

For the current program (base = {10, 22}), what is the smallest integer x such that precheck(bits, x) returns 1 and exact_check(x) returns 0?

(A) 4
(B) 5
(C) 6
(D) 7
(E) 11

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

precheck 是快速篩選:它會檢查 xx 的兩個索引位置是否都已在位元集合 bits 中。只有通過快速篩選的 xx,才會進一步執行 exact_check。

本題的索引函式為:

f1(x)=x mod 8,f2(x)=⌊x2⌋ mod 8f_1(x)=x\bmod 8,\qquad f_2(x)=\left\lfloor\frac{x}{2}\right\rfloor\bmod 8

位元集合透過 1u << p 設定第 pp 個位元;get1(bits, p) 則檢查該位元是否為 11。

解題方法

先計算 base = {10, 22} 對應的位元索引:

xxf1(x)f_1(x)f2(x)f_2(x)
1010 mod 8=210\bmod 8=2⌊10/2⌋ mod 8=5\lfloor10/2\rfloor\bmod 8=5
2222 mod 8=622\bmod 8=6⌊22/2⌋ mod 8=3\lfloor22/2\rfloor\bmod 8=3

因此,bits 中設為 11 的位置是 {2,3,5,6}\{2,3,5,6\}。通過快速篩選的條件為:

f1(x)∈{2,3,5,6}且f2(x)∈{2,3,5,6}f_1(x)\in\{2,3,5,6\} \quad\text{且}\quad f_2(x)\in\{2,3,5,6\}

依非負整數由小到大檢查:

xxf1(x)f_1(x)f2(x)f_2(x)通過 precheck?
000否
110否
221否
331否
442否
552是
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4 題3 分

Assume the blacklist policy of the firewall changes and the blacklist becomes {10, 14, 22}. To correctly apply the new policy, which parts of the program must be updated?

(A) base and exact_check
(B) exact_check only
(C) precheck only
(D) precheck and base
(E) No change is needed.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

precheck 是快速篩選:它用兩個雜湊函數,把黑名單中的數字映射到 bits 的位元位置。只有兩個位置都為 11,程式才會執行 exact_check 做精確比對。

因此,base 負責建立快速篩選所需的位元集合,exact_check 則負責判斷輸入是否真的屬於黑名單。更新黑名單時,這兩處都必須反映新增的數字;precheck 的雜湊與篩選邏輯不必改動。

解題方法

新黑名單為 {10,14,22}\{10,14,22\}。計算各數字的雜湊位置:

數字 xxf1(x)=x mod 8f_1(x)=x\bmod 8f2(x)=⌊x/2⌋ mod 8f_2(x)=\lfloor x/2\rfloor\bmod 8
1025
1467
2263

原本 base 只有 {10, 22},建立的位元集合包含位置 2,5,6,32,5,6,3,沒有包含位置 77。所以輸入 1414 會在 precheck 被擋下,無法進入精確檢查。必須把 14 加入 base,讓快速篩選設定位元 66 與 77。

同時,exact_check 也必須加入對 1414 的判斷,否則即使 1414 通過快速篩選,精確檢查仍不會將它判定為黑名單成員。

關鍵程式碼應更新為:

int base[] = {10, 14, 22};

static int exact_check(int x) {
    return (x == 10) || (x == 14) || (x == 22);
}
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 4 題共用同一段題幹

A university plans to connect all campus buildings by fiber links. Each building is a vertex (ID = 0, 1, …, n−1), and each possible fiber link is an undirected edge (a, b) with installation cost w. Assume the cost w is an integer and satisfies 0 ≤ w ≤ 10⁹. The following C program is designed to find the minimum cost of installing connections from the given links. When two edges have the same cost, the program orders them by (a, b) in increasing order (smaller a first; if a is the same, smaller b first). Answer Questions 5–8 based on the C program below.

typedef struct { int a, b, w; } E;
static int P, S;
static void f0(int n){
P = (int
)malloc(n * sizeof(int));
S = (int
)malloc(n * sizeof(int));
for(int i=0;i<n;i++){ P[i]=i; S[i]=1; }
}
static int f1(int x){
while(P[x]!=x){ P[x]=P[P[x]]; x=P[x]; }
return x;
}
static int f2(int x, int y){
x=f1(x); y=f1(y);
if(x==y) return 0;
if(S[x]<S[y]){ int t=x; x=y; y=t; }
P[y]=x; S[x]+=S[y];
return 1;
}
static int c0(const void *px, const void *py){
const E x=(const E)px, y=(const E)py;
if(x->w != y->w) return x->w - y->w;
if(x->a != y->a) return x->a - y->a;
return x->b - y->b;
}
int main(void){
int n, m;
if(scanf("%d %d", &n, &m)!=2) return 0;
E e = (E)malloc(m * sizeof(E));
for(int i=0;i<m;i++){
scanf("%d %d %d", &e[i].a, &e[i].b, &e[i].w);
if(e[i].a > e[i].b){
int t=e[i].a; e[i].a=e[i].b; e[i].b=t;
}
}
qsort(e, m, sizeof(E), c0);
f0(n);
int U = (int)malloc((n-1) * sizeof(int));
int V = (int)malloc((n-1) * sizeof(int));
int k=0;
long long total=0;
for(int i=0;i<m && k<n-1;i++){
if(f2(e[i].a, e[i].b)){
U[k]=e[i].a;
V[k]=e[i].b;
total += e[i].w;
k++;
}
}
printf("%lld %d\n", total, k);
free(e); free(U); free(V); free(P); free(S);
return 0;
}

第 5 題3 分

Which description best matches what the program does to build the backbone?

(A) It starts from one vertex and repeatedly adds the cheapest edge that leaves the visited vertices.
(B) It sorts all edges by cost, scans them in that order, and adds an edge only if it connects two different groups.
(C) It visits vertices level by level using a queue and records a tree of visits.
(D) It repeatedly selects the closest vertex based on a distance array and updates distances.
(E) It computes shortest paths between every pair of vertices, then selects edges from those paths.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考的是 Kruskal 最小生成樹演算法。把校園建築視為頂點、光纖連線視為帶權無向邊,目標是在連通所有建築的前提下,使總施工成本最低。

Kruskal 的做法是依邊權由小到大考慮每條邊;只有當邊的兩端目前屬於不同連通分量時,才加入答案。這樣可避免形成環。程式使用並查集維護連通分量:

  • f1(x):找出頂點 xx 所屬集合的代表,並透過路徑壓縮加速。
  • f2(x, y):若兩端代表不同,就合併集合並回傳 1;否則代表加入此邊會形成環,回傳 0。合併時依集合大小合併。

解題方法

程式先將每條邊的端點調整成 a≤ba \le b,再依照 (w, a, b) 排序。如此一來,邊權相同時,會依題目指定的端點順序排列。

接著逐邊掃描排序結果:

  1. 呼叫 f2(a, b) 檢查兩端是否屬於不同集合。
  2. 若不同,加入該邊、累加成本,並增加已選邊數 k。
  3. 選到 n−1n-1 條邊時停止;若圖連通,這些邊便構成最小生成樹。

程式的關鍵部分如下:

for (int i = 0; i < m && k < n - 1; i++) {
    if (f2(e[i].a, e[i].b)) {
        total += e[i].w;
        k++;
    }
}
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 6 題3 分

What is the program output for the following input?

Input:
6 11
0 1 4
0 2 3
0 3 6
1 2 5
1 4 2
2 3 7
2 5 8
3 5 1
4 5 9
1 3 10
0 4 11

(A) 16 5
(B) 16 4
(C) 15 5
(D) 15 4
(E) 14 4

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

程式使用 Kruskal 演算法求最小生成樹:先將邊依照權重由小到大排序,再依序加入不會形成環的邊,直到選出 n−1n-1 條邊,或所有邊都處理完畢。

程式以並查集判斷兩個端點是否已在同一連通分量:

  • f1(x):尋找頂點 xx 所屬集合的代表元。
  • f2(x, y):若兩端點分屬不同集合,就合併並回傳 1;若已在同一集合,代表加入此邊會形成環,回傳 0。

輸出格式為「選取邊的總權重」與「選取邊數」。

解題方法

將輸入邊依權重由小到大排序:

權重邊處理結果
1(3,5)(3,5)選取
2(1,4)(1,4)選取
3(0,2)(0,2)選取
4(0,1)(0,1)選取
5(1,2)(1,2)略過,會形成環
6(0,3)(0,3)選取

加入權重為 1、2、3、41、2、3、4 的邊後,頂點 0、1、2、40、1、2、4 已連成一個分量;頂點 3、53、5 則由邊 (3,5)(3,5) 連成另一個分量。此時邊 (1,2)(1,2) 的兩端已在同一分量,加入會形成環,因此略過。

接著選取權重為 66 的邊 (0,3)(0,3),將兩個分量連接起來。此時共有 n−1=5n-1=5 條邊,迴圈停止。

總權重=1+2+3+4+6=16\text{總權重}=1+2+3+4+6=16

選取邊數為 55,程式輸出:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 7 題3 分

The backbone is the graph formed by the k chosen edges. A vertex is an articulation point if removing it (and its incident backbone edges) disconnects the backbone. For the dataset in Question 6, which set of vertices are articulation points in the backbone?

(A) {0, 1, 4}
(B) {0, 2, 3}
(C) {1, 3, 5}
(D) {0, 1, 3}
(E) No articulation point

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

關節點(articulation point):移除該頂點及其相連的邊後,圖的連通分量會增加。若圖是一棵樹,每條邊都是橋,因此:

樹中的頂點是關節點  ⟺  它的度數≥2.\text{樹中的頂點是關節點} \iff \text{它的度數} \ge 2 .

(葉節點度數為 1,拿掉不會把其他頂點分開;度數至少 2 的頂點拿掉後,它的各個鄰居會落在不同分量。)

解題方法

第 6 題的資料集經 Kruskal 選出的骨幹邊(依加入順序):

權重邊
1(3,5)(3,5)
2(1,4)(1,4)
3(0,2)(0,2)
4(0,1)(0,1)
6(0,3)(0,3)

(權重 5 的 (1,2)(1,2) 因會成環被略過。)共 5=n−15=n-1 條邊,骨幹是一棵含 6 個頂點的樹:

      2
      |
4 — 1 — 0 — 3 — 5

各頂點在骨幹中的度數:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 8 題3 分

The operator wants one more feature: besides the current output, also print the number of connected components in the input graph (using all m edges). Print three integers in one line: total, k, c, where c is the number of connected components. Which modification is correct?

(A) Add the following code right before printf(...), and change the printf format:
int c = 0;
for(int i=0;i<n;i++){
if(f1(i) == i) c++;
}
printf("%lld %d %d\n", total, k, c);

(B) Add the following code right before printf(...), and change the printf format:
int c = 0;
for(int i=0;i<n;i++){
if(P[i] == -1) c++;
}
printf("%lld %d %d\n", total, k, c);

(C) Add the following code right before printf(...), and change the printf format:
int c = 0;
for(int i=0;i<n;i++){
if(S[i] == 1) c++;
}
printf("%lld %d %d\n", total, k, c);

(D) Add the following code right before printf(...), and change the printf format:
int c = 0;
for(int i=0;i<n;i++){
c += (P[i] == i);
}
printf("%lld %d %d\n", total, k, c);

(E) None of the above.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

題目考查並查集(Disjoint Set Union, DSU)如何表示圖的連通分量。每個集合代表一個連通分量,集合的根節點滿足 P[root] == root。因此,並查集中根節點的數量就是連通分量數。

解題方法

Kruskal 程式只在選出 n−1n-1 條邊時停止。若已選出 n−1n-1 條邊,圖已連通,連通分量數為 11;若尚未選足 n−1n-1 條邊,迴圈會掃完所有輸入邊。兩種情況下,迴圈結束時 DSU 的集合都對應輸入圖的連通分量。

因此,可以用 f1(i) == i 計算根節點,也可以直接用 P[i] == i 計算根節點。兩種寫法都正確:

int c = 0;
for(int i=0;i<n;i++){
    if(f1(i) == i) c++;
}
printf("%lld %d %d\n", total, k, c);

或:

int c = 0;
for(int i=0;i<n;i++){
    c += (P[i] == i);
}
printf("%lld %d %d\n", total, k, c);

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 9 題4 分

You need operations: insert(x), delete(x), find(x), and also “find predecessor/successor of x”. Please select all that are correct for the predecessor/successor queries.

(A) A red-black tree supports the queries in O(log n) worst case when the input is a key x.
(B) A binary heap supports the queries in O(log n) worst case when the input is a key x.
(C) A B+ tree supports the queries in O(1) average time when the input is a key x.
(D) An AVL tree supports the queries in O(log n) worst case when the input is a key x.
(E) Hashing supports the queries in O(1) average time when the input is a key x.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

前驅與後繼是有序集合中的查詢:

  • xx 的前驅:集合中小於 xx 的最大鍵值。
  • xx 的後繼:集合中大於 xx 的最小鍵值。

判斷資料結構能否有效支援這類查詢,關鍵在於它是否保留鍵值順序,以及從根節點找到 xx 所需的時間。紅黑樹與 AVL 樹都是高度為 O(log⁡n)O(\log n) 的平衡搜尋樹,因此能在 O(log⁡n)O(\log n) 最壞時間內搜尋並定位前驅或後繼。

解題方法

從根節點搜尋 xx 時,可以同時記錄候選答案。以尋找後繼為例:走到鍵值大於 xx 的節點時,該節點是候選後繼,接著往左找更小但仍大於 xx 的鍵值;走到小於 xx 的節點時,往右找。前驅則對稱處理。

successor(root, x):
    candidate = NONE
    node = root

    while node != NONE:
        if node.key > x:
            candidate = node.key
            node = node.left
        else:
            node = node.right

    return candidate
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 10 題4 分

A system needs a priority queue with frequent meld(A, B), frequent delete-min, and occasional find-max. Please select all that are correct.

(A) A Fibonacci heap is a good match for frequent meld and delete-min.
(B) A leftist heap is a good match for frequent meld and delete-min.
(C) An SMMH directly supports find-max and find-min efficiently.
(D) A Fibonacci heap supports find-max in O(1) without any extra structure.
(E) A leftist heap supports find-max in O(1) without any extra structure.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題比較三種優先佇列結構的操作效率:

  • Fibonacci heap:擅長合併與刪除最小值。合併的攤銷時間為 O(1)O(1),刪除最小值的攤銷時間為 O(log⁡n)O(\log n)。
  • Leftist heap(左偏樹):透過左偏性質限制右路徑長度,使合併與刪除最小值都能在 O(log⁡n)O(\log n) 時間內完成。
  • SMMH(Symmetric Min-Max Heap,對稱最小最大堆):同時維持最小值與最大值的結構,能在 O(1)O(1) 時間取得兩端的極值。

其中,Fibonacci heap 與 leftist heap 通常是最小堆,只有最小值位於堆頂;若沒有額外結構,不能直接以 O(1)O(1) 取得最大值。

解題方法

逐項對照各結構的支援操作與時間複雜度。題目要求頻繁合併與刪除最小值,因此合併和 delete-min 都有效率的結構符合需求;偶爾查詢最大值則不代表最小堆能直接提供 O(1)O(1) 的 find-max。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 11 題4 分

A database index is stored on disk. Minimizing disk I/O is important. Fan-out means the maximum number of children of a node. Please select all that are correct.

(A) A B-tree is suitable for disk because a node can store many keys and child pointers in one disk page/block. This gives high fan-out and small height (few page reads).
(B) A red-black tree is suitable for disk because its fan-out is 2, which reduces the number of disk page reads.
(C) A B+ tree is suitable for disk indexes for the same reason as a B-tree: high fan-out and small height.
(D) A B+ tree is often used when range queries are common, because its leaves can be linked for fast sequential scan on disk.
(E) For disk-based indexes, a red-black tree usually needs more page reads than a B-tree/B+ tree when searching for a key, because the tree is taller.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題考的是磁碟索引的設計:磁碟讀取通常以「頁」或「區塊」為單位,因此索引節點能在一頁中存放越多鍵值與子節點指標,樹的分支度(fan-out)就越高,搜尋時需要讀取的頁數通常越少。

若每個內部節點最多有 ff 個子節點,索引含有 NN 筆資料,樹高大致與下式同階:

h=O(log⁡fN)h = O(\log_f N)

在磁碟索引中,搜尋路徑上的每一層通常都需要讀取一頁。ff 越大,hh 越小,搜尋所需的頁面讀取次數也越少。

解題方法

逐項判斷各種樹的節點容量與高度:

  • B-tree 與 B+ tree 的節點可在一頁中存放多個鍵值及子節點指標,因而具有高分支度。
  • 紅黑樹是二元搜尋樹,每個節點最多只有兩個子節點。若每個節點各自存於一頁,搜尋路徑會比高分支度的樹更長。
  • B+ tree 的葉節點常以指標串接,適合依序掃描相鄰鍵值,對範圍查詢尤其有利。

選項分析

(A) 正確。
B-tree 的節點能在一個磁碟頁或區塊中存放多個鍵值與子節點指標,因此分支度高。搜尋時只需讀取少數層級的頁面,適合磁碟索引。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 12 題2 分

Consider an algorithm with a running time function T(n)=50n2+200nlog⁡n+106T(n)=50n^2+200n\log n+10^6. Is it mathematically correct to claim that T(n)=O(n2)T(n)=O(n^2)?

(A) True
(B) False

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

大 OO 表示漸近上界。若存在常數 c>0c>0 與 n0n_0,使得對所有 n≥n0n\ge n_0 都有

T(n)≤cn2,T(n)\le c n^2,

就能宣稱 T(n)=O(n2)T(n)=O(n^2)。

判斷時,固定常數項不影響漸近成長階;若每一項都能被 n2n^2 的常數倍所界定,整個函數也能被 n2n^2 的常數倍所界定。

解題方法

取 n≥2n\ge 2。對常見的對數底數,log⁡n≤n\log n\le n,因此

200nlog⁡n≤200n2.200n\log n\le 200n^2.

此外,n≥2n\ge 2 時 n2≥1n^2\ge 1,所以

106≤106n2.10^6\le 10^6n^2.

合併各項:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 13 題2 分

Consider the famous “Halting Problem,” which has been mathematically proven to be undecidable. Because the Halting Problem is strictly harder than any problem in NP, it satisfies the definition of being NP-Hard. Since the Halting Problem satisfies the NP-Hard property, it is therefore correctly classified as an NP-Complete (NPC) problem.

(A) True
(B) False

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考「NP-hard」與「NP-complete」的差別:

  • NP-hard(NP 困難):每個屬於 NP 的問題,都能在多項式時間內歸約到此問題。NP-hard 問題本身不必屬於 NP,也不必可判定。
  • NP-complete(NP 完全):問題必須同時是 NP-hard,且屬於 NP。

因此:

NP-Complete=NP-Hard∩NP\mathrm{NP\text{-}Complete}=\mathrm{NP\text{-}Hard}\cap\mathrm{NP}

屬於 NP 的問題都能由多項式時間驗證器在有限時間內驗證,因而是可判定的。停機問題已知不可判定,所以不屬於 NP。

解題方法

判斷題目中的推論是否成立,關鍵是檢查它是否漏掉 NP-complete 的必要條件。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 14 題2 分

If a decision problem belongs to the class NP, it implies that given a specific candidate solution, there exists a deterministic algorithm that can verify whether this solution is correct in polynomial time.

(A) True
(B) False

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查複雜度類別 NP 的定義。對於一個判定問題,若輸入答案為「是」,存在一份長度受輸入大小多項式限制的證明(也稱候選解或憑證),且可由確定性演算法在多項式時間內驗證,就可用「多項式時間驗證器」描述該問題屬於 NP。

形式上,判定問題 LL 屬於 NP,表示存在確定性多項式時間驗證器 VV,使得對輸入 xx:

x∈L  ⟺  ∃y, ∣y∣≤poly⁡(∣x∣), V(x,y)=1x \in L \iff \exists y,\ |y| \leq \operatorname{poly}(|x|),\ V(x,y)=1

其中 yy 是候選解,V(x,y)=1V(x,y)=1 表示驗證器確認候選解有效。

解題方法

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 15 題2 分

The 0/1 Knapsack Problem can be solved optimally by using a Greedy strategy that always selects the item with the highest value-to-weight ratio first.

(A) True
(B) False

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

0/1 背包問題中,每件物品只能「整件選取」或「完全不選」,不能拆分。若物品 ii 的重量為 wiw_i、價值為 viv_i,背包容量為 WW,目標是在總重量不超過 WW 的條件下,最大化總價值。

依「價值重量比」vi/wiv_i/w_i 由高至低選取,是分數背包問題的最佳貪婪策略;在 0/1 背包中,物品不能拆分,這項策略不保證得到最佳解。

解題方法

用反例檢驗題目所稱的策略是否對所有輸入都能得到最佳解。設背包容量為 5050,有以下三件物品:

物品重量價值價值重量比
110606
2201005
3301204
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 16 題2 分

Suppose a problem called “Robot-Path-Planning” is a known NP-Complete problem. To prove that a related new problem “3-SAT” is also NP-Complete, we must demonstrate that “3-SAT” is in NP, and we must construct a polynomial-time reduction from “3-SAT” to “Robot-Path-Planning”.

(A) True
(B) False

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

要證明問題 BB 是 NP-Complete,須證明兩件事:

  1. B∈NPB \in NP:給定一個候選解,可以在多項式時間內驗證它是否正確。
  2. BB 是 NP-hard:將一個已知的 NP-Complete 問題 AA 多項式時間歸約到 BB,也就是證明 A≤pBA \le_p B。

歸約方向的意義是:若能在多項式時間內解決 BB,便能藉由轉換後的實例,在多項式時間內解決 AA。因此,AA 至少和 BB 一樣難。

解題方法

令 AA 為已知 NP-Complete 的 Robot-Path-Planning,目標 BB 為 3-SAT。要證明 3-SAT 是 NP-Complete,應證明:

3-SAT∈NP\text{3-SAT} \in \text{NP}

並構造多項式時間歸約:

Robot-Path-Planning≤p3-SAT\text{Robot-Path-Planning} \le_p \text{3-SAT}

題幹要求的歸約方向是:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 3 題共用同一段題幹

Assuming that you are a medical information analyst, you are currently analyzing an algorithm designed to process a 3D MRI volume. The input is a cubic volume of data with side length nn (total voxels = n×n×nn \times n \times n). Assume nn is a power of 2.

Pseudo-code:

Surface_Scan(n)
total_intensity = 0
for i = 1 to n
for j = 1 to 100
total_intensity = total_intensity + Read_Pixel(i, j)
return total_intensity

MRI_Voxel_Process(n)
if n <= 1
return

for k = 1 to n
    Surface_Scan(n)

for x = 0 to 1
    for y = 0 to 1
        for z = 0 to 1
            MRI_Voxel_Process(n/2)

Assume that the function Read_Pixel(i, j) takes constant time.

第 1-(1) 題3 分

Analyze the Surface_Scan(n) function and determine its time complexity using Θ\Theta-notation.

Time complexity: ____

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

使用 Θ\Theta 記號分析時間複雜度時,要計算輸入規模 nn 增加時,基本操作執行次數的漸近成長率。常數倍不影響 Θ\Theta 階。

解題方法

Surface_Scan(n) 有兩層迴圈:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 1-(2) 題3 分

Write down the recurrence relation for the running time T(n)T(n) of MRI_Voxel_Process(n).

T(n)=T(n)= ____

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

遞迴式由兩部分組成:每次呼叫自身所需的非遞迴工作,以及遞迴子問題的數量與大小。MRI_Voxel_Process(n) 在每個非基本情況中,會呼叫 8 次規模為 n/2n/2 的子問題。

解題方法

先計算每次 Surface_Scan(n) 的工作量。外層迴圈執行 nn 次,內層迴圈固定執行 100 次;每次 Read_Pixel 為常數時間,因此一次掃描耗時 Θ(100n)=Θ(n)\Theta(100n)=\Theta(n)。

MRI_Voxel_Process(n) 會執行 nn 次掃描,所以非遞迴工作量為:

n⋅Θ(n)=Θ(n2)n \cdot \Theta(n)=\Theta(n^2)

接著計算遞迴工作:三層迴圈分別讓 x,y,zx,y,z 取 0 或 1,共產生 2×2×2=82 \times 2 \times 2=8 個子問題,每個子問題的規模都是 n/2n/2。

因此,當 n>1n>1 時:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 1-(3) 題4 分

Analyze the asymptotic growth rate of the algorithm, and determine the overall asymptotic tight bound (Θ\Theta) for the running time of MRI_Voxel_Process(n). Please write down your calculation process.

  • Calculation process: ____
  • Overall asymptotic tight bound: ____

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考察遞迴時間複雜度。先計算每次遞迴呼叫中「非遞迴工作」的成本,再建立遞迴式,最後使用主定理求出漸近緊確界。

解題方法

在 Surface_Scan(n) 中,外層迴圈執行 nn 次,內層迴圈固定執行 100100 次;每次 Read_Pixel(i, j) 為常數時間。因此:

TSurface_Scan(n)=n⋅100⋅Θ(1)=Θ(n)T_{\text{Surface\_Scan}}(n) = n \cdot 100 \cdot \Theta(1) = \Theta(n)

MRI_Voxel_Process(n) 會呼叫 Surface_Scan(n) 共 nn 次,所以這部分的成本為:

n⋅Θ(n)=Θ(n2)n \cdot \Theta(n) = \Theta(n^2)

接著,三層從 00 到 11 的迴圈各產生 22 次執行,合計呼叫 2⋅2⋅2=82 \cdot 2 \cdot 2 = 8 次規模為 n/2n/2 的子問題。因此遞迴式為:

T(n)=8T(n2)+Θ(n2)T(n) = 8T\left(\frac{n}{2}\right) + \Theta(n^2)

且 T(1)=Θ(1)T(1) = \Theta(1)。

使用主定理,a=8a=8、b=2b=2、f(n)=Θ(n2)f(n)=\Theta(n^2),並計算:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 3 題共用同一段題幹

You are a process engineer managing a critical Lithography Machine. The machine has a strict thermal constraint: you cannot schedule production for two consecutive shifts. If Shift ii is active, Shift i−1i-1 and Shift i+1i+1 must be idle (cooling).

The estimated Production Yield Value for the upcoming 13 shifts is listed below:

Shift IDS1S2S3S4S5S6S7S8S9S10S11S12S13
Value45954535854540856565403590

第 2-(1) 題4 分

Identify the set of shifts that results in the maximum possible total value while satisfying the thermal constraint.

  • Selected Shifts: ____ (e.g., S1, S2, …)
  • Total Value: ____

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題是「加權非相鄰選取」問題:每個班次有一個產值,選取的班次不能相鄰,目標是讓總產值最大。這等價於在路徑圖上找最大權重獨立集,也可用一維動態規劃求解。

令 dp[i]dp[i] 表示只考慮 S1S1 到 SiSi 時,符合限制的最大總產值。對第 ii 個班次只有兩種決策:

  • 不選 SiSi:最大值為 dp[i−1]dp[i-1]。
  • 選 SiSi:因為 Si−1S_{i-1} 必須休息,最大值為 dp[i−2]+vidp[i-2]+v_i。

因此遞迴式為:

dp[i]=max⁡(dp[i−1], dp[i−2]+vi)dp[i]=\max\bigl(dp[i-1],\ dp[i-2]+v_i\bigr)

邊界條件為 dp[0]=0dp[0]=0,且 dp[1]=v1dp[1]=v_1。

解題方法

依照上式逐班次計算:

班次 ii產值 viv_idp[i]dp[i]最佳決策
14545選 S1S1
29595選 S2S2
34595不選 S3S3
435130選 S4S4
585180選 S5S5
645180不選 S6S6
740220選 S7S7
885265選 S8S8
965285選 S9S9
1065330選 S10S10
1140330不選 S11S11
1235365選 S12S12
1390420選 S13S13

計算幾個關鍵步驟如下:

dp[4]=max⁡(95, 95+35)=130dp[4]=\max(95,\ 95+35)=130 dp[8]=max⁡(220, 180+85)=265dp[8]=\max(220,\ 180+85)=265 dp[10]=max⁡(285, 265+65)=330dp[10]=\max(285,\ 265+65)=330 dp[13]=max⁡(365, 330+90)=420dp[13]=\max(365,\ 330+90)=420

最大總產值為 dp[13]=420dp[13]=420。接著從 S13S13 往回追蹤:選 S13S13 後退兩格至 dp[11]dp[11];S11S11 不選,改取 dp[10]dp[10];選 S10S10 後退至 dp[8]dp[8];選 S8S8 後退至 dp[6]dp[6];S6S6 不選,取 dp[5]dp[5];選 S5S5 後退至 dp[3]dp[3];S3S3 不選,取 dp[2]dp[2],選 S2S2。

所以選取的班次為 S2、S5、S8、S10、S13S2、S5、S8、S10、S13,總值為:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2-(2) 題4 分

A very important client demands that Shift 7 (Value: 40) MUST be included in the schedule. Under this mandatory condition, what is the maximum total value you can achieve for the 13 shifts?

  • Selected Shifts with S7: ____
  • Total Value with S7: ____

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題是「路徑圖上的最大權重獨立集」問題:每個班次是一個節點,價值是節點權重,相鄰班次不能同時選取。題目另外要求必須選擇 S7。

固定 S7 後,S6、S8 必須停機,因此其餘可選班次分成兩段:S1~S5 與 S9~S13。兩段彼此不影響,可以各自求最大值,再加上 S7 的價值。

解題方法

對一段班次定義 DP[i]DP[i] 為前 ii 個班次在不選相鄰班次時的最大總價值。處理第 ii 個班次時,只有兩種情況:

  • 不選第 ii 個:沿用 DP[i−1]DP[i-1]。
  • 選第 ii 個:第 i−1i-1 個不能選,因此價值為 vi+DP[i−2]v_i+DP[i-2]。

所以遞迴式為:

DP[i]=max⁡(DP[i−1], vi+DP[i−2])DP[i]=\max(DP[i-1],\ v_i+DP[i-2])

左段:S1~S5

價值依序為 45,95,45,35,8545,95,45,35,85。逐步套用遞迴式:

班次範圍最大總價值
S145
S1~S295
S1~S395
S1~S4130
S1~S5180

左段選 S2、S5,總價值為 95+85=18095+85=180。

右段:S9~S13

價值依序為 65,65,40,35,9065,65,40,35,90。套用相同遞迴式:

班次範圍最大總價值
S965
S9~S1065
S9~S11105
S9~S12105
S9~S13195

右段選 S9、S11、S13,總價值為 65+40+90=19565+40+90=195。

加上必選 S7 的價值 4040:

180+40+195=415180+40+195=415

關鍵程式碼

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2-(3) 題2 分

Process Upgrade: The High-Temperature Machine Imagine the factory installs a new, high-performance machine. This machine generates significantly more heat, so the safety constraint is updated:

New constraint: After any active shift, the machine must cool down for TWO consecutive shifts.

Let f[i]f[i] be the maximum value achievable considering shifts up to ii, and let viv_i be the value of Shift ii. Please write down the complete recurrence relation (mathematical equation) for the updated condition:

f[i]=f[i]= ____

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題考的是「帶有間隔限制的最大值選擇」動態規劃。每個班次只有兩種決策:安排第 ii 班,或讓第 ii 班休息。

若安排第 ii 班,機器必須接著休息兩班,因此第 i−1i-1、i−2i-2 班都不能安排生產;前面最多只能接到第 i−3i-3 班的最佳排程。

解題方法

不安排第 ii 班時,最佳值是 f[i−1]f[i-1]。安排第 ii 班時,取得價值 viv_i,並加上截至第 i−3i-3 班的最佳值 f[i−3]f[i-3]。取兩種情況的較大者:

f[i]=max⁡(f[i−1], f[i−3]+vi)f[i]=\max\bigl(f[i-1],\ f[i-3]+v_i\bigr)

邊界可設為 f[0]=f[1]=f[2]=0f[0]=f[1]=f[2]=0;對 i=1,2i=1,2,安排當前班次時,前面可接的最佳值視為 00。這題不是選擇題,因此沒有選項需要逐一分析。

關鍵程式碼與複雜度

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 4 題共用同一段題幹

You are analyzing the routing latencies for a constellation of 4 satellites (Nodes A, B, C, D). The adjacency list gives the latency between specific nodes:

  • A → B: 10
  • A → C: −5
  • C → B: 4
  • B → D: 5
  • D → C: 2
  • D → A: 3

Requirement: You need to analyze the latency characteristics of the network so that shortest communication paths can be efficiently determined when needed.

第 3-(1) 題4 分

Based on the specific properties of the edges in this graph and the problem requirement, identify the single most appropriate standard algorithm to use. Explain your choice by pointing out the specific graph feature that disqualifies other algorithms, and explicitly state why at least one other commonly used shortest-path algorithm would fail or be inappropriate in this case.

  • Algorithm: ____
  • Why choosing this algorithm? ____

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

需求是「分析整個網路的延遲特性,讓任兩顆衛星之間的最短通訊路徑在需要時能立即查到」——這是全點對最短路徑(all-pairs shortest paths)問題,而且圖中有負權重邊 A→C=−5A\to C=-5。

解題方法

  1. 檢查負權重環:圖中的有向環

    • A→C→B→D→AA\to C\to B\to D\to A:−5+4+5+3=7-5+4+5+3=7
    • A→B→D→AA\to B\to D\to A:10+5+3=1810+5+3=18
    • C→B→D→CC\to B\to D\to C:4+5+2=114+5+2=11

    都是正的,沒有負權重環,最短路徑有明確的有限值。

  2. 選擇演算法:Floyd–Warshall 以距離矩陣為基礎,逐一允許每個節點 kk 當中繼點:

    d[i][j]←min⁡(d[i][j], d[i][k]+d[k][j])d[i][j]\leftarrow\min\bigl(d[i][j],\ d[i][k]+d[k][j]\bigr)

    它允許負權重邊(只要沒有負權重環),一次算出所有節點對的最短距離,之後任何查詢都是查表 O(1)O(1),正好符合「需要時能有效率地取得」。時間 Θ(V3)\Theta(V^3)、空間 Θ(V2)\Theta(V^2);V=4V=4 時計算量極小。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3-(2) 題2 分

Let MM be the initial 4×44 \times 4 distance matrix used at the start of your chosen algorithm. Construct and write down MM clearly.

Use “∞” for non-existing edges and “0” for self-loops. The order of nodes is A, B, C, D.

M=[‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾]M=\begin{bmatrix} \underline{\quad} & \underline{\quad} & \underline{\quad} & \underline{\quad}\\ \underline{\quad} & \underline{\quad} & \underline{\quad} & \underline{\quad}\\ \underline{\quad} & \underline{\quad} & \underline{\quad} & \underline{\quad}\\ \underline{\quad} & \underline{\quad} & \underline{\quad} & \underline{\quad} \end{bmatrix}

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題考的是以 Floyd–Warshall 演算法求所有節點對之最短路徑時,如何建立初始距離矩陣。矩陣中的 M[i][j]M[i][j] 表示節點 ii 到節點 jj 的直接邊權重;若沒有直接邊,設為 ∞\infty;自己到自己的距離設為 00。

解題方法

依照題目指定的節點順序 A、B、C、D,將每條有向邊填入對應的「起點列、終點欄」:

  • A → B 為 1010,A → C 為 −5-5
  • B → D 為 55
  • C → B 為 44
  • D → A 為 33,D → C 為 22

其餘非對角線位置沒有直接邊,填入 ∞\infty;對角線位置填入 00。因此:

M=[010−5∞∞0∞5∞40∞3∞20]M= \begin{bmatrix} 0 & 10 & -5 & \infty\\ \infty & 0 & \infty & 5\\ \infty & 4 & 0 & \infty\\ 3 & \infty & 2 & 0 \end{bmatrix}

Floyd–Warshall 後續會依序嘗試以每個節點 kk 作為中繼點,更新距離:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3-(3)(a) 題2 分

After the algorithm completes execution, what is the final shortest path cost from Node D to Node B? Please trace the path logic (e.g., D → … → B).

  • Final shortest path cost: ____
  • Final path logic: ____

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題要求節點 DD 到節點 BB 的最短路徑成本。依四個節點的全點對最短路徑情境,使用 Floyd–Warshall 演算法:逐一允許節點作為中繼點,更新任意兩點間的距離。

更新公式為:

d[i][j]=min⁡(d[i][j], d[i][k]+d[k][j])d[i][j] = \min\bigl(d[i][j],\ d[i][k] + d[k][j]\bigr)

其中 kk 是目前允許作為中繼點的節點。

解題方法與計算

初始距離設為:對角線 d[i][i]=0d[i][i]=0、有直接邊的距離設為邊權重,其餘設為無限大。

考慮經過 AA 的路徑:

D→A→C=3+(−5)=−2D \to A \to C = 3 + (-5) = -2

因此,DD 到 CC 的成本可更新為 −2-2。接著考慮經過 CC 到達 BB:

D→C→B=−2+4=2D \to C \to B = -2 + 4 = 2

對應的完整路徑是:

D→A→C→BD \to A \to C \to B

其成本為 3+(−5)+4=23 + (-5) + 4 = 2。另一條直接可見的路徑 D→C→BD \to C \to B 成本為 2+4=62+4=6,因此不如經過 AA 的路徑。

關鍵程式碼與複雜度

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3-(3)(b) 題2 分

State the asymptotic time complexity, Θ\Theta, of this algorithm.

  • Time complexity: ____

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題組選用的演算法是 Floyd–Warshall(全點對最短路徑、可處理負權重邊)。它的時間複雜度由三層迴圈決定:

for k = 1 to V
    for i = 1 to V
        for j = 1 to V
            d[i][j] = min(d[i][j], d[i][k] + d[k][j])

解題方法

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊
📄 以下 3 題共用同一段題幹

You are optimizing the core engine of an image editing tool called “Smart Lasso.” The tool helps users extract objects by automatically finding the best boundary between a Start Pixel (S) and a Target Pixel (T).

The image is represented as a grid of pixels. Each pixel has an associated “Energy Cost” (representing high-frequency noise or weak edges). The rules are:

  • Movement rule: From any pixel, you can move to adjacent pixels (Up, Down, Left, Right). Diagonal movement is not allowed.
  • Path cost: The sum of the Energy Costs of all pixels visited on the path (excluding S, but including T).

The energy cost matrix is shown below (assume the cost of entering T is 0):

[S459879869324156567T]\begin{bmatrix} S & 4 & 5 & 9 & 8\\ 7 & 9 & 8 & 6 & 9\\ 3 & 2 & 4 & 1 & 5\\ 6 & 5 & 6 & 7 & T \end{bmatrix}

Coordinates are represented as (row, col). The top-left is (0, 0).

If two neighbors have the same cost, prioritize the direction in this order: Right → Down → Up → Left.

第 4-(1) 題4 分

A junior developer implemented a “High-Speed Mode” for the lasso. The logic is simple:

“At every step, the cursor simply snaps to the immediate neighbor with the lowest energy cost to get closer to the target. It does not plan ahead.”

Trace the path generated by this method from S to T and calculate the total cost.

  • Path sequence: ____ (e.g., (0,0) → (0,1) → …)
  • Total path cost: ____

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這題考的是「局部貪婪選擇」:每一步只查看能讓游標更接近目標的相鄰格,選擇進入能量成本最低的格子,不會預先規劃整條路徑。

此處「更接近」以曼哈頓距離衡量。座標 (r,c)(r,c) 到目標 (rT,cT)(r_T,c_T) 的曼哈頓距離為

∣r−rT∣+∣c−cT∣|r-r_T|+|c-c_T|

每次移動都必須讓此距離減少 11。路徑成本則是沿途進入的像素成本總和;起點 SS 不計,目標 TT 的成本為 00。

解題方法

目標座標是 (3,4)(3,4)。從目前位置列出能縮短曼哈頓距離的鄰格,選其中能量成本最低者;若成本相同,依序使用右、下、上、左的優先順序。

目前位置能縮短距離的候選格(進入成本)選擇
(0,0)(0,0)右 (0,1)(0,1):44;下 (1,0)(1,0):77(0,1)(0,1)
(0,1)(0,1)右 (0,2)(0,2):55;下 (1,1)(1,1):99(0,2)(0,2)
(0,2)(0,2)右 (0,3)(0,3):99;下 (1,2)(1,2):88(1,2)(1,2)
(1,2)(1,2)右 (1,3)(1,3):66;下 (2,2)(2,2):44(2,2)(2,2)
(2,2)(2,2)右 (2,3)(2,3):11;下 (3,2)(3,2):66(2,3)(2,3)
(2,3)(2,3)右 (2,4)(2,4):55;下 (3,3)(3,3):77(2,4)(2,4)
(2,4)(2,4)下 (3,4)=T(3,4)=T:00(3,4)(3,4)

因此路徑為

(0,0)→(0,1)→(0,2)→(1,2)→(2,2)→(2,3)→(2,4)→(3,4)(0,0)\rightarrow(0,1)\rightarrow(0,2)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)\rightarrow(2,4)\rightarrow(3,4)

起點不計入成本,將其餘各格的成本相加:

4+5+8+4+1+5+0=274+5+8+4+1+5+0=27

關鍵程式碼

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4-(2) 題4 分

Users have complained that the “High-Speed Mode” (from Q. 4.1) often gets “trapped” in noisy areas or takes expensive detours, failing to find the true object boundary. Your manager asks you to rewrite the core engine. The new requirement is:

“The tool must guarantee finding the mathematically optimal boundary—the path with the absolute minimum Total Cumulative Cost from Start to Target—even if it takes a bit more computation time.”

Trace the path generated by this method from S to T and calculate the total cost.

  • Path sequence: ____ (e.g., (0,0) → (0,1) → …)
  • Total path cost: ____

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題要求在所有合法路徑中,找出進入像素能量成本總和最小的路徑。這是非負權重圖的單源最短路徑問題,適合使用 Dijkstra 演算法。

把每個像素視為圖上的頂點;從像素 uu 移動到相鄰像素 vv 的邊權重,設為「進入 vv 的成本」。因此,起點 SS 的成本不計,將起點距離設為 00;目標像素 TT 的成本為 00,照常納入路徑。

解題方法

從 S=(0,0)S=(0,0) 開始,逐步取出目前累積成本最小的像素,並嘗試更新它的上下左右鄰居。方向依題目指定順序設定為右、下、上、左。對每個鄰居 vv,候選成本為:

dist⁡(u)+cost⁡(v)\operatorname{dist}(u)+\operatorname{cost}(v)

沿最短路徑的關鍵累積成本如下:

像素進入該像素的成本累積成本
(0,0)(0,0)起點不計00
(1,0)(1,0)7777
(2,0)(2,0)331010
(2,1)(2,1)221212
(2,2)(2,2)441616
(2,3)(2,3)111717
(2,4)(2,4)552222
(3,4)=T(3,4)=T002222

因此,最短路徑是沿左側往下,再沿第 2 列向右,最後進入目標像素。

關鍵程式碼與複雜度

以下程式以優先佇列實作 Dijkstra;方向陣列依「右、下、上、左」排列。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4-(3) 題2 分

The solution in Q. 4.2 works great on small grids. However, when applied to a 4K image (with NN pixels), the tool becomes laggy. Profiling reveals the bottleneck is in the Open Set (Priority Queue)—specifically, the operation of repeatedly finding and removing the pixel with the minimum cumulative cost. You are comparing two underlying data structures for this Priority Queue: a simple Unsorted Array versus a Binary Min-Heap. Please analyze the time complexity (Big-O) for the “Extract-Min” operation in both cases.

  • Case 1: You use a simple Unsorted Array to store the candidate pixels. The time complexity for Extract-Min is: ____
  • Case 2: You switch to a Binary Min-Heap to store the candidate pixels. The time complexity for Extract-Min is: ____

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

優先佇列的 Extract-Min 操作,是取出目前累積成本最小的候選像素。時間複雜度取決於資料結構如何維持與尋找最小值。

以下以目前優先佇列中的候選像素數量 kk 分析;kk 最多可達影像像素總數 NN。

解題方法

Case 1:未排序陣列

陣列沒有依成本排序,因此必須逐一比較 kk 個候選像素,才能確定最小值。比較需要 O(k)O(k) 時間;找到最小值後,用最後一個元素補上空位即可在 O(1)O(1) 時間完成移除。

extractMin(array):
    minIndex = 0
    for i = 1 to array.length - 1:
        if array[i].cost < array[minIndex].cost:
            minIndex = i
    minPixel = array[minIndex]
    array[minIndex] = array[array.length - 1]
    removeLast(array)
    return minPixel

掃描成本主導整體時間,因此 Extract-Min 為 O(k)O(k)。若以 NN 表示候選像素數量上限,最壞情況為 O(N)O(N)。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

其他考古題