Editorial for COCI '14 Contest 3 #4 Coci
Submitting an official solution before solving the problem yourself is a bannable offence.
Let the ordered pair denote the contestant that won
points in the first round and
points in the second round.
Let's concentrate on a contestant . In order to find his best possible place, we need to count the contestants
that are surely better than
on the total ranking list: their number incremented by
is the required best place of contestant
.
If and
, contestant
is surely better than
. Are there any other contestants that are better? No, there aren't! The rest of the contestants can be in front of
after the first two rounds for a maximum of
points (because they weren't better than him at least in one round), so if all of them win
points in the third round,
catches up if he wins
points. Therefore, it is sufficient to only count the contestants
such that
and
.
Let us now find the worst possible place for contestant . Let us assume that he wins
points in the third round. Which contestants
can be better than him on the total ranking list? Clearly, those aren't the ones with
and
because they also win
points. Let us assume that all other contestants
win
points in the third round. In at least one round, they had more or equal points as
. If they had equal points as
, and lost from
by
points in the remaining round, after three rounds they will have the equal sum of points as
so they are not better than him. In all other cases, they are better.
In a nutshell: in the worst possible scenario for , contestants
that are NOT better than him are the ones with
and
and those with
,
,
or
,
,
. The number of such contestants subtracted from
gives us the number of contestants that are better than
so that number increased by
gives the worst possible position of contestant
after the first three rounds.
Therefore, the task comes down to counting pairs such that
and
, as well as those such that
and
. If the number of pairs
is stored in a matrix at position
, we need to be able to quickly find the sum of a rectangle in that matrix. We do this by first dynamically calculating the sums of all rectangles starting at corner
and quickly calculate the sum of any rectangle by their mutual subtractions.
Common mistakes of contestants on this task:
- some of them who used logarithmic structure (Fenwick tree) forgot that it starts from
, not from
- a lot of them didn't notice that contestant
cannot overtake contestant
even though the latter one doesn't "majorize" him. In all other "non-majorizing" cases, overtaking is possible.
Comments