Zco2024 Solutions and Cutoffs
Zco2024 Solutions and Cutoffs
1 Vegetables
Author: Soumyaditya Choudhuri
Abridged statement: There are two arrays of positive integers, A and B, both of length
N . In one operation, you can select any positive element from either array and decrement it
by one. The score is the value of N
P
i=1 Ai × Bi after all operations. What is the minimum
possible score you can achieve after applying at most Xj operations? Find the answer for Q
different values of Xj .
Constraints:
• 1 ≤ N, Q ≤ 2 · 105 .
Subtasks:
• Subtask 4 (7 points) Ai = 1.
1
• Subtask 8 (7 points) The sum of all Ai does not exceed 2 · 105 . Also, the sum of all Bi
does not exceed 2 · 105 .
Notice that it is always optimal to reduce the Ai value for which the corresponding Bi value
is maximum. Therefore, we can sort the array of B values in descending order, and we should
take the first Xj values to decrease by. This sorting will take O(N log N ) time.
For each query, we can find the largest Xj values of Bi and compute their total reduction in
at most O(N ) time.
2
1.5 Subtask 5: (18 points) N, Q, Xj ≤ 30
Notice that for each term, only the number of moves made on each of the Ai and the Bi
terms matter.
We can use dynamic programming to solve this subtask. Let dp[i][u] represent the minimum
sum of the first i terms if u upgrades have been used so far.
The base case is that the sum of the first 0 terms is 0, regardless of the number of upgrades
performed. That is, dp[0][u] = 0 for all u.
For the transition, we can iterate over x, the number of upgrades performed on A, and y, the
number of upgrades performed on B. Then, the total number of moves made is x + y. We
must ensure that A[i] >= x and B[i] >= y before trying this move, as well as u >= x + y.
There are O(X · X) transitions to consider from each state, and a total of O(N · X) states.
Therefore, the total complexity per query is O(N X 3 ), for a total complexity of O(QN X 3 ).
This can be trivially optimised to O(N X 3 ) by not recomputing the DP table every query,
given that all answers can be precomputed. However, this optimisation is not necessary to
solve this subtask.
Note that it may be possible to perform case 1 for a given x and y but impossible to perform
case 2. We will temporarily ignore the restriction that A and B must be non-negative in order
to demonstrate the result.
The final contribution of the term in each case is:
We claim that d2 ≤ d1 .
This is not hard to see as d1 − d2 = xy + y(Bi − Ai ), which is non-negative since Bi ≥ Ai
and x and y must both be non-negative. Note that when x + y moves cannot be used on Ai ,
it means it get reduced to 0 beforehand, and thus there is no point doing any further moves
on this index.
Hence, its optimal to use all moves on either Ai or Bi (whichever is smaller). This optimizes
our dynamic programming to only need O(X) transitions, thus leading to a O(N X 2 ) solution.
Note that unlike the previous subtask, you will need to precalculate all the values beforehand.
3
1.7 Subtask 7: (16 points) N, Q ≤ 103
Note that from the previous subtasks, we only apply the moves to the minimum among Ai
and Bi . Again, without loss of generality, assume Ai ≤ Bi .
Let us consider the reduction in the term Ai · Bi when we apply one operation on Ai . The
new value is (Ai − 1) · Bi , which is smaller by Bi . Thus, if we only had one upgrade possi-
ble, then its optimal to do it on the index with maximum value of Bi (again assuming Ai ≤ Bi ).
This leads us to an greedy algorithm : Use moves on the maximum value of Bi until you
exhaust it (i.e. when the corresponding Ai goes to 0). We can prove that this greedy is
correct via Exchange Arguments.
For this subtask, the only thing left to do is sort indexes according to Bi (descending order)
and use as many upgrades as possible on the first index, then the second index (if there are
still upgrades left) and then the third and so on.
1.8 Subtask 8: (7 points) The sum of all Ai does not exceed 2 · 105 .
Also, the sum of all Bi does not exceed 2 · 105
Let us try to optimize our subtask 7 solution. Note that since the sum of A values and the
sum of B values are both bounded by 2 · 105 , after at most 2 · 105 upgrades, we can get the
water requirement to 0.
Thus, we only need to simulate all upgrades, and store the answers for all of them. We can
perform the greedy algorithm once, and at each step store the sum of Ai · Bi .
When answering queries, we simply check if the number of upgrades allowed is greater than
the number of simulated steps; if it is, then the answer is 0. Otherwise, we can read the value
from our array of simulated results. Then the complexity is O(S + N log(N ) + Q), where S
is bounded by 2 · 105 .
Complexity is O(max{Xj } + N log N + Q), and note that max(Xj ) is bounded by 2 · 105 for
this subtask.
4
1.10 Subtask 10: (8 points) No additional constraints
Let Ci denote the answer for a query with i updates. Note that the value of Ci+1 −Ci changes
at a maximum of N points (i.e. when an index gets exhausted and we move on to the next one).
First, consider Ai ≤ Bi for all indices, and further the indices are sorted by Ai . Then Di
denote ij=1 Bj . Then, for x = Di , the value of Cx+1 − Cx changes from Bi to Bi+1 .
P
Let us store the values of Di and the corresponding the sums of the array. Now suppose k
is the last index such that Dk ≤ Xj and Sk is the corresponding memoized sum. Then, the
answer is simply Sk − (Xj − Dk ) · Bk+1 because from Dk onwards, the value of Ci+1 − Ci
is Bk+1 and it only changes again at Dk+1 (which we haven’t reached yet, since we defined
k to be the last index).
To implement this, you can use a map to store the memoized results, and binary search to
find the relevant value of k. Alternatively, you can store two parallel prefix sum arrays and
binary search on the arrays.
5
2 Fruits
Author: Soumyaditya Choudhuri
Constraints:
• 1 ≤ N, M ≤ 3 · 105 .
• 2 ≤ K ≤ N · M ≤ 3 · 105 .
• 1 ≤ Q ≤ 2 · 105 .
Subtasks:
• Subtask 6 (9 points) : N = 1.
6
2.2 Subtask 2 (8 points) : Q = 1, N · M ≤ 500.
Consider a graph where the nodes are the fields, and there is an edge between every pair of
adjacent fields. There are potentially O(N M ) fields that grow fruit U , and O(N M ) fields
that grow fruit V , for a total of O((N M )2 ) pairs. For each such pair, we can find the shortest
path between them in O(N M ) using a BFS.
We can check the Manhattan distance between all possible pairs of fields.
Here, we describe how to find the farthest field in the top-left quadrant. We can use an
identical algorithm in different directions to find the farthest fields in the other quadrants.
Consider a field (x1 , y1 ) with fruit U . For all fields (x2 , y2 ) in the top-left quadrant, we have
x2 ≤ x1 and y2 ≤ y1 . So, the Manhattan distance between the two fields is (x1 − x2 ) + (y1 −
y2 ) = (x1 + y1 ) − (x2 + y2 ).
In order to find the farthest field in the top-left quadrant, we need to find the field with the
smallest possible value of x2 + y2 . So, the problem reduces to finding the smallest value of
x2 + y2 among fields growing fruit V for the top-left quadrant of every field.
This can be done using dynamic programming. Let dp[i][j] be the smallest value of x2 + y2
among fields growing fruit V such that 1 ≤ x2 ≤ i and 1 ≤ y2 ≤ j (or infinity, if such a field
does not exist).
( We can compute this DP using the following recurrence:
i + j, if dp[i-1][j] == ∞ and dp[i][j-1] == ∞
dp[i][j] =
min(dp[i-1][j], dp[i][j-1]), otherwise
The time complexity is O(N M ).
7
2.4.2 Solution 2
An alternative solution to this subtask involves simply finding, for each combination of fruit
(from 1 to K) and each corner (top-left, bottom-left, top-right, bottom-right), the closest
fruit of each type to each corner. Then, this reduces the candidates to just four cells per fruit
(one for each corner). It is sufficient to check only these candidates, so a total of 4 × 4 = 16
pairs can be searched. Finding these 4 items for each fruit can be done in O(N · M ) time,
and the overall complexity of this solution is also O(N · M ).
Without loss of generality, assume that i < j. If there is some cell smaller than a that grows
fruit U , then it is optimal to switch a to that cell, so the current pair (a, b) is not optimal. This
implies that a must be the leftmost field growing fruit U . Similarly, b must be the rightmost
cell growing fruit V .
For each fruit, only the leftmost and rightmost cells are relevant. We can compute these cells
for each color in the beginning. While answering a query, we can check O(1) pairs of cells.
8
2.8 Subtask 8 (12 points) : There will be no operations of type 1.
We extend the idea from subtask 5 to two dimensions.
The absolute value of any number z is |z| = max(z, −z). Consider two cells (x1 , y1 ) and
(x2 , y2 ). The Manhattan distance between them, |x1 − x2 | + |y1 − y2 |, can be written in
another form:
We can simply find the maximum possible value of |(x1 + y1 ) − (x2 + y2 )| among all pairs of
cells and the maximum possible value of |(x1 − y1 ) − (x2 − y2 )| among all pairs of cells, and
take the maximum of both of these to find the answer.
The problem of finding the maximum possible value of |(x1 + y1 ) − (x2 + y2 )| is very similar
to subtask 5, and we use the same idea of precomputing the minimum and maximum x + y
values for all fruits. We need to do a similar precomputation for |(x1 − y1 ) − (x2 − y2 )|. It is
also possible to derive this solution via a precomputation of Solution 2 to subtask 4.
9
3 Scientific Committee
The problem setting committee consisted of, in alphabetical order:
10
4 Results and Statistics
A total of 313 contestants participated in this contest, obtaining a total of 51 distinct scores
out of 200 possible points. This section features the breakdown of results by problem, subtask,
and over the whole contest.
• Minimum score: 0
• Median score: 13
• Distinct scores: 19
The following table summarises the number of participants who solved each subtask of this
problem:
11
46 31 106 76-106
40 3 109 107-109
31 8 117 110-117
29 6 123 118-123
23 6 129 124-129
22 5 134 130-134
16 7 141 135-141
13 17 158 142-158
7 60 218 159-218
0 95 313 219-313
• Minimum score: 0
• Median score: 0
• Distinct scores: 15
The following table summarises the number of participants who solved each subtask of this
problem:
12
62 5 20 16-20
53 2 22 21-22
49 7 29 23-29
46 1 30 30
40 20 50 31-50
28 67 117 51-117
19 1 118 118
12 3 121 119-121
7 1 122 122
0 191 313 123-313
13
59 2 90 89-90
57 3 93 91-93
56 1 94 94
55 5 99 95-99
51 1 100 100
50 2 102 101-102
47 1 103 103
46 22 125 104-125
44 3 128 126-128
41 7 135 129-135
40 1 136 136
35 7 143 137-143
31 5 148 144-148
28 10 158 149-158
23 3 161 159-161
22 2 163 162-163
19 1 164 164
16 3 167 165-167
13 10 177 168-177
7 51 228 178-228
0 85 313 229-313
14
5 Cutoffs for qualification to INOI 2024
15