Hi everyone!

I've just solved 739E - Gosha is hunting in an easy but interesting way. However, it's quite different from what's described in the editorial, and I couldn't find any accepted codes that use this approach. That's why I'm curious if anyone solved this problem like me, and if this is a well-known trick :)

First, there's an easy *O*(*n*^{3}) DP solution to the problem: for each (*i*, *x*, *y*) compute the best score using *x* PokeBalls and *y* UltraBalls on *i* first Pokemon.

How to improve it? Let's randomly shuffle all Pokemon. If we looked at the first *i* of them, then we'd expect to use around PokeBalls, and UltraBalls there (*a* and *b* are the total amount of balls of each type), because the places where we used them in the optimal solution are now randomly distributed... even though positions where we use PokeBalls are not independent from positions where we use UltraBalls.

Now, using some random walk properties we can (I hope!) say, that it's really unlikely that we'll deviate from those expected values by more than . So we can just do the brutal DP, but constrain ourselves to *x* and *y* from a specific range of size . After that the solution is *O*(*n*^{2}) and get's accepted: 24153740

All opinions are welcome. Thanks :)

Is there a way to calculate the chance of failure for a given range — e.g. what's the chance that a range of size O(log(N)) or O(sqrt(N)) (your range) would get WA?

Moreover, is there a way to calculate the optimal strategy (which may involve performing the DP several times over different random arrays and taking the best answer) if we want the confidence to be above a certain number C (C > 0.9995 seems reasonable for a problem with around 100 test cases)?

For example, would it be better to perform the DP four times on a range of sqrt(N)/2 and take the best of the four answers, or perform the DP once on a range of sqrt(N)?

You can do something like a Chernoff bound on this. Let σ be permutations. Let

S_{i}be the number of Poke-balls in the firstifor a given permutation σ.So essentially by a Chernoff/Hoeffding bound,

By a union bound, the total failure probability is ≤ 2

ne^{ - C2}(because there arenvalues ofiand 2 types of balls). So evenC≈ 10 is safe.This isn't precise since we're dealing with permutations and not the sum of independent random variables, but its close enough that this should be rigorous.

So, to answer your question above, consider doubling C. Then your error probability is down to

e^{ - 4C2}. If you run it 4 times withC, then your error probability is (e^{ - C2})^{4}=e^{ - 4C2}. So its the same!