arraysalgorithmmathoptimizationlogic

I need an optimal way to iterate through the array


enter image description hereI know this a conceptual based site and is asking homework type questions is not appreciated. So before I ask for help, here is background about me: I am a competitive programmer, and I do it as my hobby.

During a recent contest, I came across this question: https://atcoder.jp/contests/abc334/tasks/abc334_c

Now the logic I used to solve this question

This problem can be considered as a problem that asks to make $\left\lfloor\frac{K}{2}\right\rfloor$ pairs from one sock each of color $A_1, A_2, \ldots, A_K$. If $K$ is even, it seems optimal that pairing $\left(A_1, A_2\right),\left(A_3, A_4\right), \ldots,\left(A_{K-1}, A_K\right)$, so that adjacent colors (in the sorted sequence) are paired, which is indeed the case.

The problem is when $K$ is odd. In this case, we can brute-force over the only color that is not paired, where the optimal way to make pairs from the other socks can be found just as we did for an even $K$. When the only color that is not paired is fixed, the minimum total weirdness when the remaining socks are paired is naively found in $O(N)$ time, but it results in a total of $O\left(N^2\right)$ complexity. Instead, one can precalculate the total weirdness when the first few socks are paired, such as presum $[2]=\left(A_2-A_1\right), \operatorname{presum}[4]=\left(A_4-\right.$ $\left.A_3\right)+\left(A_2-A_1\right)$, presum $[6]=\left(A_6-A_5\right)+\left(A_4-A_3\right)+\left(A_2-A_1\right), \ldots$, in manner of prefix sum; one can also find "suffix sum." Thus, this way, the problem can be solved in a total of $O(N)$ time.

When we use this approach and try to solve the question, we get a TLE error due to the time constraints of the problems. On checking the editorial, the only way to get rid of this TLE is to use this optimization:

When $K$ is odd, instead of brute-forcing ever sock that is not paired, one can only try $A_1, A_3, A_5, \ldots, A_K$ . In the following sample code, we adopt this method to simplify the implementation.

I am not able to understand one why need not try A2, A4 and all other even indexed elements? Can someone help me or atleast hint with an logic behind this optimization. Thank you

Edit: I'm unable to fix my latex here, could someone kindly help with that too. I have added an image of the algo I used. And whatever I am trying to explain using latex.


Solution

  • Let's say you leave out an even numbered sock x. All the socks on either side, except its neighbors must be paired with their adjacent socks, so around x, you end up with a situation like:

    a x b
    

    If you leave out x, you have to pair a and b, and the cost is b-a. If you leave out a or b instead, you always get a better result, with cost x-a or b-x.

    ... so you never need to consider leaving out the even-numbered socks.

    Note, however, that even without this optimization, your algorithm should be linear time, and there should be no chance of getting TLE given the constraints in the problem.