Editorial for Expensive Chair Stacking


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Plasmatic

I wanted to add a series of quick solution sketches to my problems without one. These aren't technically full editorials, but I hope that they are enough for you if you are stuck. :-)

The first two subtasks were meant to reward various brute force approaches.

The third subtask was solvable by considering the x and y axes separately. Consider the x axis: if we let f(x) be the total payment of moving all chairs to position x, we note that x is convex. Thus, we can binary search for the best position x (and repeat for the y axis).

The final subtask extends on the third subtask a bit, noting that the position where f(x) is maximized is either 0 or M, allowing us to solve the problem in linear time. As N is very large, be careful with implementation.


Comments

There are no comments at the moment.