You are on page 1of 4

Editorials

Drugs

Problem
You are a drug dealer. You have a list of potential buyers. Each buyer has a budget, and will buy the drugs
if the price is less than or equal to his/her budget.
You want to set a price to maximise the revenue. Find the maximum possible revenue.

Editorial
Claim: Only a price that is exactly the budget of at least one customer can lead to an optimal solution.
Proof : Consider a price p, that is not the budget of a customer, and assume that p is optimal. Say m
people can afford the drugs are this price. Now, consider the lowest budget that is strictly greater than p
say q, and consider the revenue when the price is set at q. Since the same number of people can afford the
drugs, the revenue is q · m, which is strictly greater than p · m.
Now, we have a feasible brute force solution, we can iterate over all the budgets, and calculate the profit for
each of these by iterating over all the other budgets. This is O(N 2 ).
We can do better by realizing that if we sort the list of budgets, we will have a guarantee that for a price p,
every budget to its left is either equal to or greater than it. Hence, for the element at position i in the sorted
array, revenuei = (N − i) · valuei . This is O(N · log N ), and is good enough to pass all the test cases.

1
Find

Problem
Alice had an array A of size N + 1, and ∀i, Ai ∈ {1, 2, 3, . . . , N }. She remembers that there existed exactly
one pair i, j such that Ai = Aj , that is, A contained exactly one element from {1, 2, 3, . . . , N } twice, and
every other element exactly once.
Sadly, Alice has forgotten A and N . She remembers only
N
!
Y
p= Ak mod 109 + 7
k=0

She is now interested in all the possible values of A, such that |A| ≤ 106 + 1.
Consider the set S of all arrays A which have p as their product. Print the sum of the extra elements (the
element which is present twice) of all A ∈ S

Editorial
Consider an array A of size N (and not N + 1), which each number from {1, 2, 3, . . . , N } once. The value of
p for A is clearly N ! mod (109 + 7). But we are interested in the product of the arrays which have an extra
element, say x. Then the p = (N ! · x) mod (109 + 7). Now, we have a range on N, which is between 1 and
106 . We can iterate over this range, and generate factorials in constant time (per N ). So if we can compute
x for a given p and N quickly, then we are done. By modular arithmetic

(N !·x) ≡ p (mod 109 + 7)


=⇒ x ≡ p · (N !)−1 (mod 109 + 7)

Now, since 109 + 7 is prime, we can compute the inverse of N ! using Fermat’s little theorem, and fast
modular exponentiation. The solution runs in O(N · log (109 + 7))

2
Game

Problem
Alice and Bob are playing a turn-based game. They have a tree T , and each edge has a strength associated
with it.
On her turn, Alice selects any one edge, and decreases its strength by 1.
On his turn, Bob selects any one edge, and increases its strength by 1.
When the strength of an edge reaches 0, it is destroyed. Note that destroyed edges cannot be revived.
The score after every move is calculated as the number of distinct pairs (i, j) such that i and j are connected.
You are the scorekeeper for this game. After every move, print the score. Note that Alice moves first, and
then the players alternate.

Editorial
Note that initially, the number of pairs is N2 , because everything is connected to everything. Now, consider


simulating the entire game, breaking all the edges as mandated by the moves to arrive at a final graph F .
Now, consider the components of F . If we know the sizes of these components, we can compute the number
of connected pairs. Now, we proceed in the reverse direction, joining componenents together as and when
we need to.
Why does this help? Because we know efficient ways to join two components together, and find the number
of new connected pairs this creates, using the Disjoint Set Union data structures. The setter’s solution
for this is O(N · log N + M ). Better implementations can shave the log N factor to the inverse Ackermann
function.

3
Diwali Lights

Problem
Asheesh is buying Diwali lights this year, and while testing the array of lights, he observed something. If
there are N light bulbs in the array, and m of them are on at time t, then, with uniform probability one of
them will switch off at time t + 1. At the same time, with some probability the bulb to its left will toggle
(no other bulbs will be affected). The process continues until all the bulbs are off.
Asheesh went to the shopkeeper for replacement as after some time all the bulbs stopped functioning. But
before going he wonders what is the expected time for the entire array to go dark, given which bulbs are on
at t = 0.

Editorial
We begin by developing a recurrence relation for the time t(k) of a state k, in which m bulbs are on. If a
1
bulb i is on, then it will be switched off with probability m . And if i is not the left-most bulb, the bulb on
its left will toggle with probability pi−1 . Hence, for each i which is on, and not the left-most bulb, we add

t(kt ) · pi−1 + t(knt ) · (1 − pi−1 )


m
where kt is the state in which i is off, and i − 1 has been toggled, and knt is the state in which i is off, and
i − 1 is not toggled. If i is the left-most bulb, it suffices to assign a dummy value of 0 to pi−1 in the relation
above to express the sum correctly.
Finally, we can memoize this to obtain a solution with complexity O(2N ).

You might also like