Editorial : Make it Palindrome 2
The solution has three short ideas stacked on top of each other:
- Reduce to pair-gaps. Only the gaps between symmetric pairs matter.
- Move to the difference array. Range-add problems usually become point-change problems after differencing.
- Minimum-cost signed representation. A clean bookkeeping finishes the problem in one sorted pass.
We explain each step with the first sample in hand:
$$ N = 4,\; M = 5,\; A = [0,\, 3,\, 1,\, 2]. $$A palindrome means $A_i = A_{N+1-i}$ for every $i$. The middle element (when $N$ is odd) pairs with itself, so it is automatically fine and we ignore it.
For each of the $h = \lfloor N/2 \rfloor$ pairs, define the cyclic gap
$$ b_i \;=\; (A_i - A_{N+1-i}) \bmod M \;\in\; \{0,1,\ldots,M-1\}. $$The palindrome condition becomes: every $b_i = 0$.
In the sample, pairs are $(0, 2)$ and $(3, 1)$, giving $b = [3,\; 2]$.
Take any operation on $A$: “add $1$ mod $M$ to every index in $[l, r]$.” For a pair $(i,\; N+1-i)$, exactly one of four things happens:
- Range covers both elements → both go up by $1$ → $b_i$ unchanged.
- Range covers only $A_i$ → $b_i$ goes up by $1$ (mod $M$).
- Range covers only $A_{N+1-i}$ → $b_i$ goes down by $1$ (mod $M$).
- Range covers neither → $b_i$ unchanged.
Because symmetric pair-indices move in opposite directions as $i$ grows, the set of pairs that change turns out to be a contiguous interval of pair-indices, and all pairs in that interval change the same way (all $+1$ or all $-1$):
- An operation entirely in the left half adds $+1$ to some contiguous range of $b$.
- An operation entirely in the right half adds $-1$ to some contiguous range of $b$.
- A middle-crossing operation adds $+1$ or $-1$ to a contiguous range of $b$ (or changes nothing).
Conversely, every “$+1$ to $b$ on range $[p, q]$” can be realized as operation $[p, q]$ on $A$ (stays in the left half), and every “$-1$ to $b$ on range $[p, q]$” can be realized as the mirrored operation $[N+1-q,\; N+1-p]$ on $A$ (stays in the right half).
So the problem is exactly equivalent to:
Starting from a vector $b \in (\mathbb{Z}/M)^h$, one operation adds $+1$ or $-1$ (mod $M$) to a contiguous range of $b$. Minimum operations to reach the zero vector.
Classic trick. Pad $b$ with zeros on both ends, $(0, b_1, \ldots, b_h, 0)$, and take consecutive mod-$M$ differences:
$$ d_0 = b_1,\qquad d_i = (b_{i+1} - b_i) \bmod M,\qquad d_h = (0 - b_h) \bmod M. $$So $d$ has $m = h + 1$ entries in $[0, M)$.
One range-add on $b$ only changes $d$ at the two ends of the range — it takes one unit from one position of $d$ and gives it to another. So our reformulated problem becomes:
Use unit-transfers between positions of $d$ to make every entry equal to $0$ mod $M$. Minimize the number of transfers.
In the sample, $b = [3, 2]$ extends to $(0, 3, 2, 0)$, so
$$ d = \bigl((3-0)\bmod 5,\; (2-3)\bmod 5,\; (0-2)\bmod 5\bigr) = [3,\; 4,\; 3]. $$If we wrote each difference without wrapping, the “true” integer differences would telescope to $0$ (because $b$ is padded with $0$ on both sides). Wrapping each into $[0, M)$ adds some nonnegative multiple of $M$. Therefore
$$ \sum_{i=0}^{m-1} d_i \;=\; K \cdot M \qquad\text{for some integer } K \ge 0. $$In the sample, $3 + 4 + 3 = 10 = 2 \cdot 5$, so $K = 2$.
Each $d_i$ is a residue mod $M$ and can represent one of two signed integers:
- keep positive: signed value $+d_i$;
- flip negative: signed value $-(M - d_i)$.
Unit-transfers preserve the signed sum, so to land on the zero vector we need the chosen signed values to sum to $0$.
If we flip $f$ entries (turning each $d_i$ into $-(M - d_i)$), the signed sum equals
$$ \sum d_i - f \cdot M \;=\; (K - f)\cdot M, $$which is $0$ iff $f = K$. So we must flip exactly $K$ entries.
Think of the problem as transporting units between piles. Each unflipped pile supplies $d_i$ units; each flipped pile demands $M - d_i$ units. Both sides have the same total (that is why $f = K$). Minimum unit-transfers equals the total supply (equivalently, total demand):
$$ \text{transfers} \;=\; \sum_{\text{unflipped}} d_i. $$We want this as small as possible, so we should flip the $K$ largest entries of $d$ — i.e. keep the $m - K$ smallest. The answer is
$$ \boxed{\text{answer} \;=\; d_{(0)} + d_{(1)} + \cdots + d_{(m - K - 1)}}, $$where $d_{(0)} \le d_{(1)} \le \cdots$ is $d$ sorted ascending.
Our $d = [3, 4, 3]$, $K = 2$, $m - K = 1$. Sorted: $[3, 3, 4]$. Sum of smallest $1$ entry is $3$, which is the expected answer.
For sample 3, $N = 7$, $M = 12$, $A = [3, 1, 4, 1, 5, 9, 2]$: ignore middle element, pairs give $b = [1, 4, 11]$. Extended $(0, 1, 4, 11, 0)$ gives
$$ d = [1,\; 3,\; 7,\; 1] \pmod{12}. $$Sum $12$, $K = 1$, $m - K = 3$. Sorted $[1, 1, 3, 7]$, sum of smallest $3$ is $1 + 1 + 3 = 5$. Matches.
- Lines building
w: $w = (0,\; b_1,\; \ldots,\; b_h,\; 0)$. - Lines building
d: mod-$M$ consecutive differences. sort(all(d))sorts $d$ ascending.k = (int)d.size() - accumulate(all(d), 0LL) / Mis exactly $m - K$.accumulate(d.begin(), d.begin() + k, 0LL)sums the smallest $m - K$ entries.
- Per test case $O(N \log N)$ due to the sort; fits easily under $\sum N \le 2 \cdot 10^5$.
- Use $64$-bit integers for $\sum d$: individual $d_i < M \le 10^9$ and $m$ can be up to $10^5$.
- Watch the edge case $N = 1$: no pairs, $b$ is empty, $d = [0]$, and the answer is $0$.