Data Structures & AlgorithmsVery hardICPC regional · GATE CS
Piles of stones [3, 5, 1, 2, 6] are in a row. Each move merges two ADJACENT piles into one, at a cost equal to their combined number of stones. What is the minimum total cost to merge everything into a single pile?
- A. 41
- B. 37
- C. 17
- D. 38
Correct answer: B. 37
Interval DP: the last merge splits the row into a left block and a right block, and each block is merged optimally first; cost adds the total stones in the interval. Always merging the cheapest adjacent pair is a greedy trap.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- In how many different ways can the product of 5 matrices A·B·C·D·E be fully parenthesised (grouping by brackets, keeping the order of…
- Two players alternately take a pile from either END of the row [4, 15, 3, 9, 2, 8]; the first player moves first and each takes the whole…
- You may partition the multiset [3, 1, 4, 1, 5, 9, 2, 6] into as many GROUPS as possible, where every group must have sum at least 7 and…
- From the numbers [7, 2, 9, 4, 1, 6, 3], choose the largest possible number of elements (each used at most once) whose total is at most 12…
- Choose two different elements of [-8, 1, 5, 9, 12, 20, 33] whose sum is as close as possible to the target 30. What is that sum?
- Each turn you pick a number x from [3, 4, 2, 4, 4, 6], earn x points, and then delete EVERY copy of x−1 and x+1. You may keep doing this…