New Rating
- Problem link: this
- Implemented the dynamic programming solution I read yesterday.
2033C - Sakurako's Field Trip
Another straightforward question that I was not able to solve.
We do not need to care about the placement of the elements at the start(and therefore also at the end) of the array, since they can only be influenced by the elements in front and behind them respectively. Instead we can just swap $$$a_1$$$ and $$$a_{n - 1}$$$ elements instead. Therefore we can keep $$$a_0$$$ and $$$a_n$$$ as is.
For an index $$$i$$$ we can compare the disturbance before and after operating on it wrt to the $$$(i - 1)^{th}$$$ and $$$(n - i)^{th}$$$ elements respectively.
But What about the $$$(i + 1)^{th}$$$ and $$$(n - i - 1)^{th}$$$ elements?
Those elements will be compared to $$$i^{th}$$$ and $$$(n - i)^{th}$$$ element respectively and will be appropriately handled(Use the same logic as used for $$$a_0$$$ and $$$a_n$$$ elements, instead of adjusting these elements for their neighbours we did the opposite, adjusting the neighbours for these elements.)