B - SCSC Card Game Editorial
by
Lulusphere
One person remaining in the line can be regarded as representing some contiguous interval.
Among all cards owned by that person, only two values are important:
\[ (s,c)=(\text{minimum spade},\ \text{minimum club}). \]
When two adjacent intervals are merged, the resulting state can also be determined only from these two values.
Suppose the two states are \((s_1,c_1)\) and \((s_2,c_2)\).
In an S-game, the side with the larger minimum spade wins. Therefore, the resulting state is
\[ (s_1,c_1),(s_2,c_2) \rightarrow (\max(s_1,s_2),\min(c_1,c_2)). \]
In a C-game, the side with the larger minimum club wins. Therefore, the resulting state is
\[ (s_1,c_1),(s_2,c_2) \rightarrow (\min(s_1,s_2),\max(c_1,c_2)). \]
Let the final state be \((S_f,C_f)\). The objective value is
\[ (S_{\mathrm{sum}}-S_f)(C_{\mathrm{sum}}-C_f). \]
If \(S_f\) is fixed, this value becomes smaller as \(C_f\) becomes larger. Hence, for each possible value of \(S_f\), it is enough to find the maximum possible value of \(C_f\).
Fix the final spade value as \(p\).
Call a member large if \(S_i\ge p\), and small otherwise. For the final minimum spade to be \(p\), the spade cards of small members must not remain until the end. Therefore, all small members must eventually lose in S-games.
Consider a maximal contiguous segment consisting only of small members. This segment can be merged into one state using only C-games. Its state becomes
\[ (\min S_i,\ \max C_i). \]
Then, by playing an S-game with an adjacent large member, the small spade value disappears, while its club value can be passed to the large member.
Let \(L\) and \(R\) be the leftmost and rightmost positions among members with \(S_i\ge p\).
All members outside \([L,R]\) are small. Let \(A_L\) be the maximum club value in the segment left of \(L\), and let \(A_R\) be the maximum club value in the segment right of \(R\). If such a segment is empty, regard the corresponding value as \(\infty\).
First, consider the case \(L<R\).
A large member strictly inside \((L,R)\) does not have to directly handle the small segments outside. Thus, such a member can keep its own club value as a candidate for \(C_f\):
\[ \max_{L<i<R,\ S_i\ge p} C_i. \]
Now consider the two endpoint large members \(L\) and \(R\).
If \(L\) is responsible for the final club value, it must handle the small segment on the left. Since the left small segment can be compressed into a state with club value \(A_L\), the best club value that can remain through \(L\) is
\[ \min(C_L,A_L). \]
Similarly, the right endpoint \(R\) gives the candidate
\[ \min(C_R,A_R). \]
Therefore, when \(L<R\), the maximum possible value of \(C_f\) is
\[ \max\left( \max_{L<i<R,\ S_i\ge p} C_i,\, \min(C_L,A_L),\, \min(C_R,A_R) \right). \]
All small segments inside \([L,R]\) can be removed by S-games with adjacent large members. Then the remaining large members can be merged using C-games, so the maximum club value among the candidates can be preserved.
Now consider the case \(L=R\).
Then there is only one member with \(S_i\ge p\). This single member has to handle both the left and right small segments. Hence, the maximum possible club value is
\[ \min(C_L,A_L,A_R). \]
We try every member \(i\) as the one determining the final spade value, that is, set \(p=S_i\).
For each \(p\), compute the maximum possible \(C_f\) using the cases above. Then the candidate answer is
\[ (S_{\mathrm{sum}}-p)(C_{\mathrm{sum}}-C_f). \]
The minimum over all candidates is the answer.
The total time complexity is \({\cal O}(N^2)\).
posted:
last update:
