Editorial for COCI '23 Contest 2 #3 Dizalo
Submitting an official solution before solving the problem yourself is a bannable offence.
Prepared by: Vito Anić
In this editorial, if we call someone person
If a person wants to get out of the elevator and in front of them there are other people, they will have to go out too. But the best way they can return to the elevator is that they are sorted by the floor on which they want to exit. The closest to the door would be someone who wants to exit soonest.
Let us now solve subtask
Observe that we can split people into two groups. People who will exit the elevator just once, and people who will exit the elevator multiple times. For example: 4 5 2 1 6 3 7
: people
Let us now look at the people who exit only once. With them, everyone who wants to leave on a later floor and is in front of them will have to exit when they leave. That can be calculated with Segment or Fenwick tree. When we know that for everyone, the solution will be
In the example above, because of
Now we know how to solve
For the full solution of this task, we need to maintain the property that people in the elevator form a permutation, and somehow be able to the set of people who exit the elevator only once. For the former when erasing some element, using Segment or Fenwick trees we can maintain the values and indices. When erasing a person who exits multiple times, we must reduce the solution by the number of people who exit once are behind them and exit at a smaller floor. When erasing a person who exits the elevator only once, more people can enter that set of people. The easiest way to maintain that set is to calculate offline for every person how long will they be in the set. Total time complexity is
Comments