Editorial for IOI '15 P4 - Horses


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.

To solve the first subtask, we need to just calculate our answer using dynamic programming.

dp[i][j] - what is the maximal profit if we pass i1 days and we have j horses, then we got j×xi horses and check all number of horses that we sell today and go to the next day.

Observation 1

First of all, let's consider two points i and j. What if we sell k horses in the ith and jth day? And check which day is more profitable.

Profit of ith day: x1×x2××xi×yi

Profit of jth day: x1×x2××xi×xi+1××xj×yj

It depends on this:

yi?xi+1××xj×yj><=

  • if it's > then it's more profitable to sell horses in ith day
  • if it's < then it's more profitable to sell horses in jth day
  • if it's = then it doesn't matter if we sell them in ith day or in jth day

From this point, it's clear that it is better to sell all our horses in one day that gives us maximal profit.

Using this observation, we can solve 2 subtasks.

After each query, we can solve the problem in O(n).

Observation 2

If all xi2, then after 30 days, xi×xi+1××xi+29230>109, so position less than or equal n30 can never be an optimal solution, because even if yn30=109 and yn=1, 230×yn>yn30.

It's enough to check only the last 30 positions.

Observation 3

The main problem in the last subtask is xi=1, but in that case, multiplication first xi numbers and xi1 is not changed. This allows us to merge consecutive positions with xi=1. When we merge this position, we should take the maximal yi. So if we do that, it's enough to check the last 60 positions, because there can be no more than 30 merged 1's between 30 last positions where xi2.

So we can store our state in some structure like set, that provides us information about current merged positions, and some structure that provides us RMQ. So solution per query will be log(109)log(n). Then to calculate the answer, we can use something like segment tree.

So overall complexity will be O(mlog(109)log(n)).


Comments

There are no comments at the moment.