## Editorial for DMOPC '20 Contest 4 P4 - Javelin Throwing

**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.**

Authors:

,Let be the total number of holes on the -th throw. Notice that . Hence, as the s are fixed, we can minimize by minimizing .

#### Subtask 1

For subtask 1 we can solve for each prefix separately in for a total run-time of .

We can not have less holes in a day than in the past, so transform the array to its prefix max in . For each , .

The difference of holes between two days is at most . We can do an scan starting from the suffix to satisfy this. For each , .

**Time complexity:**

#### Subtask 2

We can optimize the above approach.

On the -th throw, one of only two things can happen:

- The javelin lands in the -th closest hole from Alice. No new hole is made.
- The javelin lands between the -th and -th holes, so a new hole is made. (It doesn't actually matter where in between.)

Since Option 1 results in fewer total holes, we want to take Option 1 as many times as possible.

We can do this greedily. Keep a running total , the current minimum possible value of . The minimum value of can easily be computed from as we go along.

On the -th throw:

- If , we can take Option 1 and make no hole. In this case, .
- If , we must take Option 2 and make a hole in front of all the current holes. In this case, .

But what if ? Then we must take Option 2, as well as change previous Option 1s to Option 2s, because we don't have enough holes.

Changing throw from Option 1 to Option 2 would add 1 to each of , hereby adding to . Hence, to minimize , we want to change the most recent Option 1s. We can do this by keeping a stack of the throws where we took Option 1, popping the most recent throws as needed.

Since each throw is pushed and popped at most once, and the rest of the computation can be done in , the overall time complexity is .

**Time complexity:**

*Problem credit: modified from https://codeforces.com/problemset/problem/924/c*

## Comments