Thank you for participating in Kick Start 2020 Round F.
Cast
ATM Queue: Written by Vikash Dubey and prepared by Raihat Zaman Neloy.
Metal Harvest: Written by Pablo Heiber and prepared by Raihat Zaman Neloy.
Painters' Duel: Written by Swante Scholz and prepared by Anson Ho.
Yeetzhee: Written by Pablo Heiber and prepared by Jonathan Irvin Gunawan.
Solutions, other problem preparation, reviews and contest monitoring by Andrey Anurin, Anoopam Mishra, Anson Ho, Anushi Maheshwari, Bartosz Kostka, Bohdan Pryshchenko, Cristhian Bonilha, Devanshu Agarwal, Diksha Saxena, Gagan Kumar, Ian Tullis, Jared Gillespie, Jayant Sharma, Jonathan Irvin Gunawan, Kevin Tran, Lalit Kundu, Lizzie Sapiro, Marcin Wawerka, Michał Łowicki, Naranbayar Uuganbayar, Nikhil Hassija, Pablo Heiber, Phil Sun, Raihat Zaman Neloy, Ruoyu Zhang, Sai Surya Upadrasta, Sanyam Garg, Saurabh Joshi, Seunghyun Jo, Sudarsan Srinivasan, Swante Scholz, Swapnil Gupta, Timothy Buzzelli, Vikash Dubey, Vipin Singh, Wajeb Saab, and Yang Xiao.
Analysis authors:
There are N people numbered from 1 to N, standing in a queue to withdraw money from an ATM. The queue is formed in ascending order of their number. The person numbered i wants to withdraw amount Ai. The maximum amount a person can withdraw at a time is X. If they need more money than X, they need to go stand at the end of the queue and wait for their turn in line. A person leaves the queue once they have withdrawn the required amount.
You need to find the order in which all the people leave the queue.The first line of the input gives the number of test cases T. T test cases follow.
The first line of each test case gives two space separated integers: the number of people standing in the queue, N and the maximum amount X that can be withdrawn in one turn. The next line contains N space separated integers Ai.
For each test case, output one line containing Case #x: y
, where x
is
the test case number (starting from 1) and y
is the space separated list of integers that denote the order in which the people leave the queue.
Time limit: 20 seconds.
Memory limit: 1 GB.
1 ≤ T ≤ 100.
1 ≤ N ≤ 100.
1 ≤ Ai ≤ 100.
1 ≤ X ≤ 100.
1 ≤ N ≤ 105 for at most 10 test cases. For the remaining cases, 1 ≤ N ≤ 100
1 ≤ Ai ≤ 109.
1 ≤ X ≤ 109.
2 3 3 2 7 4 5 6 9 10 4 7 2
Case #1: 1 3 2 Case #2: 3 5 1 2 4
In Sample Case #1, there are 3 people and the limit to withdraw in one turn is 3. Below is step-by-step description of how the process will look like:
In Sample Case #2, there are 5 people and the limit to withdraw in one turn is 6. Below is step-by-step description of how the process will look like:
You are in charge of deploying robots to harvest Kickium from a nearby asteroid. Robots are not designed to work as a team, so only one robot can harvest at any point of time. A single robot can be deployed for up to K units of time in a row before it returns for calibration, regardless of how much time it spends on harvesting during that period. Harvesting can only be done during specific time intervals. These time intervals do not overlap. Given K and the time intervals in which harvesting is allowed, what is the minimum number of robot deployments required to harvest at all possible times?
The first line of the input gives the number of test cases, T. T test cases follow.
The first line of each test case gives two space separated integers N and K: the number of time intervals in which harvesting is allowed, and the maximum duration a robot can be deployed for before returning for calibration. The next N lines contain a pair of space separated integers Si and Ei: the start time and the end time of the i-th interval respectively. Please note that intervals don't include the time unit starting at the moment Ei, so for example an interval with (Si = 2; Ei = 5) has duration of 3 time units.
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is the number of times robot deployment is needed so that for each interval there is one robot harvesting at that time.
Time limit: 20 seconds.
Memory limit: 1 GB.
1 ≤ T ≤ 100.
All Si are distinct.
For any two intervals (Si,Ei) and (Sj,Ej) with Si < Sj, Ei < Sj.
1 ≤ N ≤ 100.
1 ≤ K ≤ 100.
1 ≤ Si < Ei ≤ 200.
100 < N ≤ 105, in at most 10 test cases.
1 ≤ N ≤ 100, in the remaining test cases.
1 ≤ K ≤ 109.
1 ≤ Si < Ei ≤ 109.
2 3 5 1 5 10 11 8 9 3 2 1 2 3 5 13 14
Case #1: 2 Case #2: 3
In Sample Case #1, we deploy the robot at time instant 1 and it becomes available for the interval [1, 6]. However, it harvests only for the time range [1, 5]. After that we deploy the robot at 6 and it becomes available for the time interval [6, 11]. This deployment covers both the remaining intervals [8, 9] and [10, 11]. There are multiple optimal strategies here. For example, we can deploy the second robot at 7. It would then cover the range [7, 12], thus harvesting for the intervals [8, 9] and [10, 11].
In Sample Case #2, we deploy the robot at time instant 1, and it becomes available for [1, 3], but harvests for [1, 2] as [2, 3] is not part of any interval. After that we deploy the robot at 3 for the time range [3, 5] in which the robot harvests for the interval [3, 5]. The third deployment is done at time instant 13 making the robot available for time range [13, 15]. However, it harvests only for the interval [13, 14]. Thus three deployments are needed to cover all the intervals.
A new art museum is about to open! It is a single-story building in the shape of a large equilateral triangle. That triangle is made up of many smaller identical equilateral-triangle-shaped rooms, and the side length of the museum is S times the side length of any one of the rooms. Each room has doors connecting it to all other rooms with which it shares a side (not just a vertex).
Each room is identified by two numbers: the row of the building it is in (counting from top to bottom, starting from 1), followed by its position within that row (counting from left to right, starting from 1). Here is an example of how the rooms are connected and labeled when S = 3:
Alma and Berthe are artists who are painting the rooms of the museum. Alma starts in the room (RA, PA), and Berthe starts in a different room (RB, PB). Each of them has already painted their starting room. C of the other rooms of the museum are under construction, and neither Alma nor Berthe is allowed to enter these rooms or paint them.
Alma and Berthe are having a friendly competition and playing a turn-based game, with Alma starting first. On a painter's turn, if their current room is adjacent to at least one unpainted room that is not under construction, the painter must choose one of those rooms, move to it, and paint it. Otherwise, the painter cannot move and does nothing on their turn. Once both painters are unable to move, the game is over. The score of the game is the number of rooms painted by Alma minus the number of rooms painted by Berthe.
Both painters make optimal decisions, with Alma trying to maximize the score and Berthe trying to minimize the score. Given this, determine the best score Alma can guarantee for the game, regardless of what Berthe does.
The first line of the input gives the number of test cases, T. T test cases follow. Each case begins with one line containing six integers S, RA, PA, RB, PB, and C. Respectively, these are the side length of the museum (as a multiple of the side length of a room), the row and position of Alma's starting room, the row and position of Berthe's starting room, and the number of rooms that are under construction. Then, there are C more lines. The i-th of these lines (counting starting from 1) contains two integers Ri and Pi, representing the row and position of the i-th room that is under construction.
For each test case, output one line containing Case #x: y
, where x
is
the test case number (starting from 1) and y
is the best score that Alma can
guarantee for the game, as described above.
Time limit: 40 seconds.
Memory limit: 1 GB.
0 ≤ C ≤ S2 - 2.
1 ≤ RA ≤ S.
1 ≤ PA ≤ 2 × RA - 1.
1 ≤ RB ≤ S.
1 ≤ PB ≤ 2 × RB - 1.
(RA, PA) ≠ (RB, PB).
1 ≤ Ri ≤ S, for all i.
1 ≤ Pi ≤ 2 × Ri - 1, for all i.
(Ri, Pi) ≠ (RA, PA),
for all i.
(Ri, Pi) ≠ (RB, PB),
for all i.
Either Ri < Ri+1, or
Ri = Ri+1 and Pi < Pi+1,
for all i < C.
T = 48.
S = 2.
1 ≤ T ≤ 100.
2 ≤ S ≤ 6.
2 2 1 1 2 1 0 2 2 2 1 1 2 2 1 2 3
Case #1: 2 Case #2: 0
In Sample Case #1, the turns must proceed as follows:
Alma has painted 3 rooms and Berthe has painted 1 room, so the score is 3 - 1 = 2.
In Sample Case #2, neither painter can move. They only paint their starting rooms.
The following additional cases could not appear in Test Set 1, but could appear in Test Set 2.
2 3 3 4 2 1 2 2 3 3 1 3 3 2 2 3 2 2 1 3 1
The correct output for these two cases would be:
Case #1: 0 Case #2: -1
In Case #1, Alma can move to (3, 5) or (3, 3). She cannot move to (2, 3), which is under construction.
So Alma knows that moving to (3, 3) guarantees a score of 0 no matter what Berthe does, which is better than the score of -1 that she would get by moving to (3, 5). Therefore, Alma moves to (3, 3). Notice that:
In Case #2, Alma must move to (3, 3), and then it is better for Berthe to move to (3, 4) than to (2, 2).
Pommel is very bored at home so she has invented a new game involving N dice. Each die has the numbers from 1 to M written on it. Whenever she throws a die, it has an equal probability of landing on each of the M possible values.
Pommel places all the dice in a row. She goes through the dice one at a time from left to right. For each die she rolls, Pommel can either keep the value she rolled and move on to the next die or she can re-roll the die. Pommel can re-roll a die as much as she wants before moving on to the next die.
Once Pommel has gone through all the dice, the game is finished. To determine if she has won, she puts the dice into groups. All dice with the same value are put into the same group. So if she finishes the game with x distinct values, then there will be x groups. These groups of dice are then sorted by number of dice in non-decreasing order.
For example:
Pommel wins if she finishes the game with exactly K groups, and the i-th group contains exactly Ai dice, for all i.
What is the expected value of the total number of dice rolls it will take Pommel to win the game, assuming she plays optimally to minimize this expected value?
It is guaranteed that for any valid input, it is possible for Pommel to win the game.
The first line of the input gives the number of test cases, T. T test cases follow. The first line of each test case contains the integers N, M and K. Then, K lines follow describing the groups she must finish with. The i-th line contains Ai.
For each test case, output one line containing Case #x: y
, where x
is the test case number (starting from 1) and y
is
the expected number of times it will take to roll all the dice for Pommel to win the game.
y
will be considered correct if it is within an absolute or
relative error of 10-6 of the correct answer.
See the FAQ for an explanation of what that means,
and what formats of real numbers we accept.
Time limit: 20 seconds.
Memory limit: 1 GB.
1 ≤ T ≤ 100.
1 ≤ K ≤ M.
1 ≤ Ai, for all i.
A1 +
A2 +
... +
AK = N.
Ai ≤ Ai+1, for all i.
2 ≤ N ≤ 6.
2 ≤ M ≤ 6.
2 ≤ N ≤ 50.
2 ≤ M ≤ 50.
2 3 6 2 1 2 5 2 1 5
Case #1: 4.7 Case #2: 9.0
In Sample case #1, Pommel has N = 3 dice, each with a number from 1 to M = 6 written on them. To win, she must finish the game with K = 2 groups. One group must have one die (A1 = 1), while the other group must have two dice (A2 = 2). One optimal strategy for Pommel is as follows:
In Sample case #2, Pommel has N = 5 dice, each with a number from 1 to M = 2 written on them. To win, she must finish the game with K = 1 group, with all N dice in them (A1 = N). For the first die, Pommel rolls it once. Then, for each remaining die she keeps rolling until it has the same value as the first one. It takes 2 dice rolls on average.
This strategy takes Pommel 9 (1 + 2 + 2 + 2 + 2) dice rolls on average.
Firstly, denote Ki as the number of times a person will use the ATM. Formally, Ki = ⌈Ai / X⌉.
We can directly simulate the process using a queue.
Assume that i-th person, that wants to withdraw Ai, is first in the queue. There are two possibilites:
Time complexity of this simulation is O(Σ Ki).
In the worst case, when X = 1, Ki = Ai. Since Ai ≤ 100, the worst time complexity is O(N × 100), which easily fits into the time limit.
In the second test set, Ki can be as big as 109, so direct simulation is too slow.
Let's look at two people i and j. When will i-th person leave the queue before j-th person? There are two cases:
This observation is enough to form a full solution. Sort people first in ascending order of Ki, and in case of ties in ascending order of their number. After sorting, this is our answer.
Time complexity of this solution is O(N log N).
Consider given time intervals as an array.
We will refer to the start time of i-th interval as Si and to the end time of i-th interval as Ei.
Let us sort the time interval array by start time in ascending order. To start the harvesting, we need to deploy our first robot at the start time of the first interval (S1) in the sorted array. It is not optimal to deploy the robot before S1 because we would be wasting robot hours.
Per the problem statement, our first robot can be deployed from S1 to S1 + K before it returns for calibration.
Let us define interval length of i-th interval as interval_lengthi = Ei - Si.
The following cases are possible for our first robot:
Based on the above scenarios, the optimal strategy is to deploy a robot at the beginning of the first harvesting interval, for K hours. If after K hours the robot is inside the first or any subsequent harvesting interval, deploy another robot to replace it. Otherwise, deploy another robot as soon as the next harvesting interval begins. Repeat this for every interval and keep count of the number of total deployments.
For this test set we can loop over sorted time intervals and then have another inner loop to go over each point in time of such interval.
At the i-th point in time, if there is no robot currently deployed for harvesting, we can deploy a robot and keep it harvesting for K units of time.
If a robot is already currently deployed for harvesting then no action is needed.
The total number of robots deployed is the required answer.
Since we are sorting the intevals array and then iterating over each point in time inside an interval, the worst case time complexity will be O( Σ1 ≤ i ≤ N interval_lengthi + N log N), where N is the total number of intervals and interval_lengthi ≤ 200.
To solve for this test case we need to find a way to count the number of robots needed for an interval without iterating over each point in time.
Let us say we deploy a robot at S1. The number of robots needed for the first inteval can be calculated as ⌈interval_length1 / K ⌉.
Let us define a variable last_harvest_time as the last point in time covered by all deployed robots and initialise it as 0.
It is possible that the last robot deployed to cover the first interval will go beyond E1 hence the last_harvest_time after covering the first interval will be, last_harvest_time = max(last_harvest_time , S1) + number of robots needed for the first interval * K.
For subsequent intervals:
Since we are sorting the intervals array and then iterating over all intervals at once, the worst case time complexity will be O(N log N), where N is the total number of intervals.
There are only 4 rooms, (1, 1), (2, 1), (2, 2) and (2, 3). Both the players, will try to paint the room (2, 2), painting which would block the other player to paint more rooms.
Thus there are five cases,
At any point during the game, few rooms of the museum might be under construction, few might have been painted by Alma, few might have been painted by Berthe and a few might be unpainted.
The rooms which are either under construction, painted by Alma or painted by Berthe are not paintable any further. Lets represent these rooms as blocked rooms, and the remaining rooms as free rooms. Thus, without losing any information we can uniquely represent the state of the museum with the following parameters,
We can represent blocked/free rooms of the museum as a binary string, 0 denoting the blocked rooms, and 1 representing the free rooms.
Lets define a function, CalcScore()
, which takes the binary string, position of Alma and Berthe, and whose turn it is as input parameters and returns the optimal score as output, that is, the minimal possible score if it is Berthe's turn and maximum possible score if it is Alma's turn.
CalcScore()
recursively calls itself with every reachable state, that is, the player tries to paint all the three neighbouring rooms, thus, recursively call CalcScore()
for all the valid states.
Lets denote T(N) as time complexity when N rooms are free.
In every nested call of CalcScore()
, the number of free rooms decreases by 1, thus the recursion depth of CalcScore()
can be at max S2-2.
A room has at most three neighboring rooms, but if it is not the starting position at least one of them will already be painted because that's where the player came from. Thus, when a player reaches a room, he has only two neighboring rooms to move to.
So, every call to CalcScore()
can generate at max 2 recursive calls to CalcScore()
.
Thus,
Therefore, the time complexity of the initial configuration is O(2S2).
Which although seems to be not good enough for S = 6, but in practice, the solution passes well within the time limits as most of the recursion depth will be way less than the presumed maximum. Depends on the implemetation details, but the number of calls to CalcScore()
for the worst case is of the order of 105.
Although not needed for Test Set 2, but, we can use techniques like Memoization and Alpha–Beta pruning to further speed up the solution.
First, note that Pommel can always win the game in a finite number of moves on expectation. To do so for a given input sequence A1, A2, ... AK, Pommel can simply reroll until the first A1 dice land on 1, the next A2 dice land on 2, and so on, with the last AK dice landing on K. Such an outcome satisfies the group arrangement required by the input; furthermore, on each roll, Pommel has a 1/M chance of rolling the needed value for the die she's on. This implies that Pommel is expected to win in N×M turns. Although this may not be optimal, it demonstrates that Pommel always has a winning strategy.
The basic procedure for calculating the expected value is the same for any approach. Suppose Pommel has already rolled and fixed some (potentially zero) dice. Furthermore, suppose she chooses a set S of dice configurations, such that she will not re-roll her current die if her dice configuration, after her current roll, is in S. If there is a pi probability that her current dice configuration leads to the i-th element of S, then there is a 1 - Σ pi probability that Pommel will have to re-roll. If we now let ei equal the expected number of moves to win starting from state i, assuming Pommel plays optimally, we can solve for the expected number of moves to win starting from Pommel's current dice configuration. If we let this quantity be x:
x = 1 + (1 - Σ pi)x + Σ piei
x = (1 + Σ piei) / (Σ pi)
Approaches to the two tests sets differ in how they may enumerate the possible dice configurations and how they find the optimal set S for any given configuration.
For this test set it's sufficient to look at all dice roll results directly. There are at most Σ0≤i≤N Mi < 60,000 such possibilities. For a given configuration, performing another roll leads to one of M new dice configurations, so for each configuration we can loop through all 2M-1 possible non-empty subsets of configurations that Pommel could choose to not re-roll on (ignoring subsets that contain configurations that make it impossible for Pommel to reach a winning configuration). For each choice of subset, we compute Pommel's expected turns until winning, and we take the subset with the lowest expected value.
We need to iterate over the dice configurations such that when we're computing the minimum expected value for a configuration C, we know the minimum expected values for each configuration that C can lead to. This can be done, for example, with DFS. The expected value from the empty configuration is our answer. This algorithm runs in O(MN×2M×M) time which is sufficient.
As an example, consider the test case N = 3, M = 2, K = 2, A1 = 1, A2 = 2. Let's compute Pommel's expected turns from winning when she's rolled and locked down a 1. There are two possibilities for the dice configuration after her next turn: [1, 1] and [1, 2], each occuring with probability 1/2. As a precondition to computing the expected turns to win from [1], we know that, on expectation, it takes two moves to win from [1, 1] and one move to win from [1, 2]. We now consider the three possibilies for S: {[1, 1]}, {[1, 2]}, and {[1, 1], [1, 2]}.
S = {[1, 1]} leads to the equation x = 1 + (1 - 0.5) x + 0.5 · 2, which results in x = 4.
S = {[1, 2]} leads to the equation x = 1 + (1 - 0.5) x + 0.5 · 1, which results in x = 3.
S = {[1, 1], [1, 2]} leads to the equation x = 1 + (1 - 0.5 - 0.5) x + 0.5 · 1 + 0.5 · 2, which results in x = 2.5. This is the lowest value, so we now know that if Pommel has rolled a single 1, she will win in 2.5 more turns on expectation if she plays optimally.
For this test set, the number of possible dice roll outcomes, 5050, is far too large to be enumerated in time. We instead focus on the groupings of dice that could possibly lead to the desired configuration. For example, if Pommel rolls [1, 1, 2, 2] or if she rolls [3, 4, 3, 4], either way, she'll have two groups of two dice, which are equivalent for this problem. A dice grouping can be expressed as a K-tuple of integers (x1, x2, ..., xK) such that the tuple elements are non-decreasing: xi ≤ xi+1 for 1 ≤ i < K. The i-th element of the tuple expresses the number of dice in the i-th smallest dice group, which is zero if there are fewer than K non-empty groups in the current dice configuration. For example, if K=3 and the dice results so far are [2, 2, 3, 2, 2, 3], the grouping can be expressed as (0, 2, 4).
We need to count how many possible K-tuples we need to consider to get an estimate of our algorithm's runtime. One simple bound is that we only need to consider a tuple T if the sum of T's elements is at most K. The total number of tuples we need to consider is therefore at most Σ0≤i≤K p(i) where p(i) is the number of K-tuples whose elements are non-decreasing and add to i. p(i) can be calculated with a script, but it is also the i-th partition number. The sum of the first 50 partition numbers is only slightly more than a million, which is very tractable.
A dice roll could lead Pommel from a given configuration to at most K new configurations. We now need a way of computing the optimal subset of new configurations S to not re-roll that is faster than the naive O(K × 2K) approach. Consider the expected value equation shown above. First, note that if for some i, ei > x, removing configuration i from S will decrease the value of x. Similarly, if there is some configuration C with expected value E[C] < x that is not included in S, adding it to S will lower x. This implies that if some configuration C with expected value E[C] is in S, then all states C' with E[C'] < E[C] must also be in S if Pommel is playing optimally. We can therefore sort all configurations reachable from a given configuration so that they're in ascending order of expected turns until winning. It is guaranteed that the optimal S is a prefix of the sorted list, so it can be found by sorting and iterating over the list in O(K log K) time.
Combining our improved optimal-subset routine with our bound for the number of K-tuples (which was in fact quite loose and can be shown to be significantly lower) gives us an algorithm that runs in time for M, N ≤ 50.