Data Structures & AlgorithmsVery hardCodeforces Div 2 · ICPC regional
A chain of 5 matrices M1 to M5 has dimensions given by the sequence p = [8, 7, 16, 17, 9, 9], where Mi has size p[i−1] × p[i] (1-indexed). Multiplying two matrices of sizes a×b and b×c costs a·b·c scalar multiplications. What is the minimum total number of scalar multiplications over all parenthesisations?
- A. 4944
- B. 5673
- C. 5337
- D. 4046
Correct answer: D. 4046
The interval DP gives 4046. Multiplying strictly left to right costs 4944; the greedy 'cheapest adjacent pair first' rule gives 5673, which is not optimal.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Directed acyclic graph on vertices A, B, C, D, E, F, G (every edge goes from a smaller to a larger letter) with weighted edges A→E (2)…
- Directed acyclic graph on vertices A, B, C, D, E, F, G (every edge goes from a smaller to a larger letter) with weighted edges A→G (4)…
- Directed weighted graph on vertices A, B, C, D with edges C→B (-4); C→A (-1); A→C (1); D→A (-4); A→D (-2); C→D (0). Source A. Run…
- Connected undirected graph on vertices A, B, C, D, E, F, G, H with edges A–E (1), B–D (1), B–E (1), C–H (1), D–G (1), D–H (1), E–F (1)…
- Array [12, 23, 8, 18, 15, 14, 26, 13] is turned into a min-heap in place with bottom-up heapify: for i from ⌊n/2⌋−1 down to 0, sift the…
- Coins of denominations [11, 9, 8, 3] are available in unlimited quantity. What is the minimum number of coins needed to pay exactly 28?…