#P15615. [ICPC 2021 Jakarta R] Maxdifficent Group

[ICPC 2021 Jakarta R] Maxdifficent Group

题目描述

Given an array of integers A1..NA_{1..N} where N2N \geq 2. Each element in AA should be assigned into a group while satisfying the following rules.

  • Each element belongs to exactly one group.
  • If AiA_{i} and AjA_{j} where i<ji < j belongs to the same group, then AkA_{k} where ikji \leq k \leq j also belongs to the same group as AiA_{i} and AjA_{j}.
  • There is at least one pair of elements that belong to a different group.

Let GiG_{i} denotes the group ID of element AiA_{i}. The cost of a group is equal to the sum of all elements in AA that belong to that group.

$$\text{cost}(x) = \sum_{i \text{ s.t. } G_{i} = x} A_{i} $$

Two different group IDs, GiG_{i} and GjG_{j} (where GiGjG_{i} \neq G_{j}), are adjacent if and only if GkG_{k} is either GiG_{i} or GjG_{j} for every ikji \leq k \leq j. Finally, the diff()\text{diff}() value of two group IDs xx and yy is defined as the absolute difference between cost(x)\text{cost}(x) and cost(y)\text{cost}(y).

$$\text{diff}(x, y) = |\text{cost}(x) - \text{cost}(y)| $$

Your task in this problem is to find a group assignment such that the largest diff()\text{diff}() value between any pair of adjacent group IDs is maximized; you only need to output the largest diff()\text{diff}() value.

For example, let A1..4={100,30,20,70}A_{1..4} = \{100, -30, -20, 70\}. There are 88 ways to assign each element in AA into a group in this example; some of them are shown as follows.

  • G1..4={1,2,3,4}G_{1..4} = \{1, 2, 3, 4\}. There are 33 pairs of group IDs that are adjacent and their diff()\text{diff}() values are:
    • $\text{diff}(1, 2) = |\text{cost}(1) - \text{cost}(2)| = |(100) - (-30)| = 130$,
    • $\text{diff}(2, 3) = |\text{cost}(2) - \text{cost}(3)| = |(-30) - (-20)| = 10$, and
    • $\text{diff}(3, 4) = |\text{cost}(3) - \text{cost}(4)| = |(-20) - (70)| = 90$.

The largest diff()\text{diff}() value in this group assignment is 130130.

  • G1..4={1,2,2,3}G_{1..4} = \{1, 2, 2, 3\}. There are 22 pairs of group IDs that are adjacent and their diff()\text{diff}() values are:
    • $\text{diff}(1, 2) = |\text{cost}(1) - \text{cost}(2)| = |(100) - (-30 + (-20))| = 150$, and
    • $\text{diff}(2, 3) = |\text{cost}(2) - \text{cost}(3)| = |(-30 + (-20)) - (-20)| = 70$.

The largest diff()\text{diff}() value in this group assignment is 150150.

The other 66 group assignments are: G1..4={1,1,1,2}G_{1..4} = \{1, 1, 1, 2\}, G1..4={1,1,2,2}G_{1..4} = \{1, 1, 2, 2\}, G1..4={1,2,2,2}G_{1..4} = \{1, 2, 2, 2\}, G1..4={1,1,2,2}G_{1..4} = \{1, 1, 2, 2\}, G1..4={1,1,2,3}G_{1..4} = \{1, 1, 2, 3\}, and G1..4={1,2,3,3}G_{1..4} = \{1, 2, 3, 3\}. Among all possible group assignments in this example, the maximum largest diff()\text{diff}() that can be obtained is 150150 from the group assignment G1..4={1,2,2,3}G_{1..4} = \{1, 2, 2, 3\}.

输入格式

Input begins with a line containing an integer NN (2N1000002 \leq N \leq 100\,000) representing the number of elements in array AA. The next line contains NN integers AiA_{i} (106Ai106-10^{6} \leq A_{i} \leq 10^{6}) representing the array AA.

输出格式

Output contains an integer in a line representing the maximum possible largest diff()\text{diff}() that can be obtained from a group assignment.

4
100 -30 -20 50
150
5
12 7 4 32 9
46
6
-5 10 -5 45 -20 15
70

提示

Explanation for the sample input/output #2

The maximum possible largest diff()\text{diff}() of 4646 can be obtained from the group assignment G1..5={1,1,1,1,2}G_{1..5} = \{1, 1, 1, 1, 2\}. The diff()\text{diff}() value of the only adjacent group IDs is: diff(1,2)=46\text{diff}(1, 2) = 46.

Explanation for the sample input/output #3

The maximum possible largest diff()\text{diff}() of 7070 can be obtained from the group assignment G1..6={1,2,2,2,3,4}G_{1..6} = \{1, 2, 2, 2, 3, 4\}. The diff()\text{diff}() values of any two adjacent group IDs are: diff(1,2)=55\text{diff}(1, 2) = 55, diff(2,3)=70\text{diff}(2, 3) = 70, and diff(3,4)=35\text{diff}(3, 4) = 35.