C - AB vs. BA Editorial by evima
By reversing \(S\) and swapping A and B if necessary, we may assume that the number of Bs is at most the number of As.
We also represent \(S\) as an elevation profile, that is, a polyline in which each A is the segment \((i, h) \leftrightarrow (i + 1, h+1)\) and each B is the segment \((i, h) \leftrightarrow (i + 1, h-1)\). Let the height of the left end be \(0\) and the height of the right end be \(H\).
When the absolute height difference from an endpoint exceeds the maximum so far, we call this a “new record.”
- A
Bthat sets a new downward record, as seen from the left end, is called a Bart-leaningB. - A
Bthat sets a new upward record, as seen from the right end, is called an Abel-leaningB.
Let \(P\) be the number of Abel-leaning Bs and \(Q\) be the number of Bart-leaning Bs. The answer is as follows:
- If \(P > Q\), Abel wins.
- If \(P < Q\), Bart wins.
- If \(P = Q\), Abel wins if the number of
Bs is odd, and Bart wins if it is even.
We call a part corresponding to AB a “mountain,” and the height at the point between these two characters the summit height. Abel’s move removes one mountain. Mountains can be classified into the following three types according to what happens when they are removed.
- The number \(P\) of Abel-leaning
Bs decreases by \(1\). - The number \(Q\) of Bart-leaning
Bs increases by \(1\). - Neither \(P\) nor \(Q\) changes.
In particular, \(P\) and \(Q\) never change at the same time. To see this, let \(x\) be the summit height. For \(Q\) to increase, height \(-x\) must appear to the right of the mountain. On the other hand, for \(P\) to decrease, every height to the right must be greater than \(H-(x-H)=2H-x\).
We call a B that leans toward neither side an “unmarked B,” and a mountain that changes neither \(P\) nor \(Q\) a “neutral mountain.”
Suppose we show that “if the number of unmarked Bs is odd, a neutral mountain exists.” Then Abel can stay in a winning state by the following strategy.
- If \(P - Q \ge 2\), any move works.
- If \(P - Q = 1\), remove a neutral mountain if one exists.
- If \(P - Q = 0\), a neutral mountain exists, so remove it.
We show that if there is no neutral mountain, the number of unmarked Bs is even.
Let \(x\) be the summit height of a mountain, and let \(y\) be the height reached after descending through all the consecutive Bs from the summit. Then the following hold.
- A mountain that increases the Bart-leaning count: \(y \le -x\), and only the part from \(x\) down to \(-x\) is unmarked.
- A mountain that decreases the Abel-leaning count: \(y \le H\), and only the part from \(H+(H-y)\) down to \(y\) is unmarked.
Given these, the numbers of unmarked Bs in each descent are \(2x\) and \(2(H-y)\), respectively. Both are even, so the total number of unmarked Bs is also even.
A mountain that increases the Bart-leaning count
To increase the Bart-leaning count, the path must reach \(-x\) while staying below height \(x\). So if the descent turns back upward before reaching \(-x\), every mountain up to the point where \(-x\) is reached is neutral.
A mountain that decreases the Abel-leaning count
Let \(z\) be the summit height of the next mountain to the right (or the height of the right end, if there is none). For that mountain not to be neutral, we need \(y \le H - (z - H) \le H\).
posted:
last update: