RepeatedHalving
SRM 840 · 2022-10-24 · by misof
Problem Statement
You have a collection of bags, each containing some marbles.
You are given the
Consider the following process:
- Find the bag that currently contains the most marbles. (If there are several bags tied for having the maximum number of marbles, pick any one of them.)
- Remove half of the marbles from that bag. Round the number of removed marbles up if necessary. (E.g., if there are 21 marbles in the bag, you remove 11 and leave 10 in the bag.)
- Throw away the removed marbles.
You are going to repeat the above process steps times.
Determine the final numbers of marbles in the bags.
Return a
Constraints
- bags will contain between 1 and 100 elements, inclusive.
- Each element of bags will be between 0 and 10^9, inclusive.
- steps will be between 1 and 10^9, inclusive.
{1, 2, 3, 21, 5, 4}
4
Returns: {1, 2, 2, 2, 3, 4 }
We select the bag with 21 marbles and remove 11 of them, leaving 10. We select the bag with 10 marbles and remove 5 of them, leaving 5. We select either one of the two bags with 5 marbles and remove 3 of them, leaving 2 in that bag. We select the other bag with 5 marbles (which is now the only bag with that many marbles) and we remove 3 of them, leaving 2. Remember that the return value must be sorted. E.g., the array {1, 2, 3, 2, 2, 4} has the correct pile sizes but not in the correct order, and thus it would not be accepted as an answer.
{1, 1, 1, 1, 1}
5
Returns: {0, 0, 0, 0, 0 }
In each step we select a different bag and we remove the one marble it contains.
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
1000000000
Returns: {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
In each step we select an arbitrary bag and we remove half of the marbles it contains. (I.e., we remove zero marbles and keep zero marbles in the bag.) Keep in mind that a straightforward simulation of 10^9 steps, one at a time, will take quite a lot of time to complete.
{123, 4568, 89123, 45, 678901234}
27
Returns: {45, 123, 1142, 1294, 1392 }
{1000000000}
29
Returns: {1 }
Submissions are judged against all 73 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class RepeatedHalving with a public method vector<int> simulate(vector<int> bags, int steps) · 73 test cases · 2 s / 256 MB per case