I - Soldier Game
Basic Information
Contest | The 2018 ICPC Asia Qingdao Regional Contest |
Team AC Ratio | 6/373 (1.6%) |
Tutorial
Let's describe the problem in another way.
Use segments of length \(1\) or \(2\) to cover the whole sequence. The value of a segment is the sum of elements it covers. Minimize the difference between the maximum value and the minimum value.
Let's solve this problem using segment trees. Let \(f(l, r, x \in \{0, 1\}, y \in \{0, 1\})\) indicate the smallest max-value to cover the sub-array \(a_l, a_{l + 1}, \cdots, a_r\). More precisely:
- \(x = 0\) indicates that the segment covering \(a_l\) is completely included in interval \([l, r]\). That is, the segment covering \(a_l\) is either \([l, l]\) or \([l, l + 1]\).
- \(x = 1\) indicates that the segment covering \(a_l\) is not completely included in interval \([l, r]\). That is, the segment covering \(a_l\) is \([l - 1, l]\).
- \(y = 0\) indicates that the segment covering \(a_r\) is completely included in interval \([l, r]\). That is, the segment covering \(a_r\) is either \([r, r]\) or \([r - 1, r]\).
- \(y = 1\) indicates that the segment covering \(a_r\) is not completely included in interval \([l, r]\). That is, the segment covering \(a_r\) is \([r, r + 1]\).
We can get the recursive equation on segment tree:
where \(m = \lfloor\frac{l + r}{2}\rfloor\). The answer is \(f(1, n, 0, 0)\). The initial values are:
- \(f(i, i, 0, 0) = a_i\), this is the segment of length \(1\).
- \(f(i, i, 0, 1) = a_i + a_{i + 1}\), this is the segment of length \(2\).
- \(f(i, i, 1, 0) = -\infty\), the cost covering \(a_i\) is calculated in the previous interval.
- \(f(i, i, 1, 1) = +\infty\), \(a_i\) cannot be covered by two segments.
Next, we enumerate the min-value \(v\), and remove all segments whose value is smaller than \(v\) (no need to really remove them, just change their value to \(+\infty\)), now the answer becomes \(f(1, n, 0, 0) - v\).
There are \((2n - 1)\) possible values of \(v\). For each \(v\) modify the value of segments with segment tree and calculate the value of \(f\). The time complexity is \(\mathcal{O}(n\log n)\).
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|