Data Structures & AlgorithmsVery hardCodeforces Div 2 · ICPC regional
A chain of 5 matrices M1 to M5 has dimensions given by the sequence p = [11, 8, 3, 12, 16, 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. 3576
- B. 3060
- C. 1569
- D. 4356
Correct answer: C. 1569
The interval DP gives 1569. Multiplying strictly left to right costs 4356; the greedy 'cheapest adjacent pair first' rule gives 3576, 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
- Undirected graph on vertices A, B, C, D, E, F with weighted edges A–B (4), A–C (7), A–E (3), B–C (6), B–E (4), B–F (2), C–D (7), C–F (2)…
- Undirected weighted graph on vertices A, B, C, D, E with edges A–D (1), B–D (2), B–E (1), C–D (1), C–E (2), D–E (3). An edge is CRITICAL…
- Strings a = "accabb" and b = "aaab". The edit distance allows insert, delete and substitute, each costing 1 (the standard Levenshtein…
- Undirected graph on vertices A, B, C, D, E, F with edges A–B (1), A–E (1), B–C (1), B–D (1), B–E (1), B–F (1), C–D (1), D–E (1) (weights…
- Undirected weighted graph on vertices A, B, C, D, E with edges A–B (7), A–C (1), A–D (5), A–E (9), B–D (4), B–E (4), C–D (7), C–E (2), D–E…
- On an 6 × 6 chessboard with rows and columns numbered 0 to 5, a knight starts at (4, 4) and must reach (2, 0) (as (row, column)). A knight…