公式

C - Beware of Overflow 解説 by evima


To have only one integer on the blackboard, we need to perform exactly \(N-1\) additions.

When there are two or more integers left on the blackboard, which two should we add first? Actually, we should add the maximum and minimum values. We can see that the absolute value of their sum does not exceed \(R\) as follows.

Let \(X\) be the multiset of integers written on the blackboard, with the maximum value \(M\) and the minimum value \(m\). In particular, the absolute value of every element in \(X\) is at most \(R\), and the sum satisfies \(\displaystyle\left|\sum_{x\in X}x\right|\leq R\).

  • If \(m \geq 0\), all integers on the blackboard are non-negative. Thus, \(0 \leq m + M \leq \sum_{x\in X}x \leq R\). Therefore, \(|m + M| \leq R\).
  • If \(M \leq 0\), all integers on the blackboard are non-positive. Thus, \(0 \geq m + M \geq \sum_{x\in X}x \geq -R\). Therefore, \(|m + M| \leq R\).
  • If \(m \leq 0\) and \(M \geq 0\), then \(-R \leq m \leq m + M \leq M \leq R\), so \(|m + M| \leq R\).

This covers all cases. Therefore, we should add the maximum and minimum values.

Using merge sort etc., we can sort the values on the blackboard in ascending order through comparison interactions. After that, we perform addition interactions to add the minimum and maximum values. We remove the first and last elements from the sorted list and insert the sum of those minimum and maximum values. We can use binary search to find the insertion position, which can also be achieved through comparison interactions.

Let’s estimate the number of interactions. Suppose we can evaluate that merge sort on a list of length \(2^n\) requires at most \(a_n\) comparisons. Clearly, \(a_0 = 0\). For \(n \geq 1\), during merge sort on a list of length \(2^n\), we first divide it into two halves of \(2^{n-1}\) elements each and recursively sort them using merge sort. The number of comparisons up to this point is at most \(2a_{n-1}\). Next, we merge the two sorted lists of length \(2^{n-1}\) by repeatedly comparing the first elements and removing the first element. During the merge, at most \(2^n\) comparisons are made. Therefore,

\[a_n = 2a_{n-1} + 2^n.\]

Let \(b_n = a_n / 2^n\). Then, \(b_0 = 0\) and \(b_n = b_{n-1} + 1\), so \(b_n = n\), and we get \(a_n = n2^n\). Therefore, merge sort on a list of length \(2^n\) requires at most \(n2^n\) comparisons. Under the constraint \(N \leq 1000 \leq 2^{10}\), at most \(a_{10} = 10240\) comparisons are needed.

Next, let’s estimate the number of comparisons needed to find the insertion position for the sum. Using binary search, we can find the insertion position in a sorted list of length \(2^n - 1\) with \(n\) comparisons. Although the list shortens with each of the \(N-1\) insertions, for simplicity, we overestimate the length of the sorted list as \(2^{10} - 1 = 1023\). Then, at most \(10 \times (N-1) \leq 9990\) comparisons are needed.

In total, we need at most \(10240 + 9990 = 20230\) comparisons, and with at most \(N-1 \leq 999\) additions, we comfortably stay within the limit of \(25000\) interactions.

投稿日時:
最終更新: