#P15615. [ICPC 2021 Jakarta R] Maxdifficent Group
[ICPC 2021 Jakarta R] Maxdifficent Group
题目描述
Given an array of integers where . Each element in should be assigned into a group while satisfying the following rules.
- Each element belongs to exactly one group.
- If and where belongs to the same group, then where also belongs to the same group as and .
- There is at least one pair of elements that belong to a different group.
Let denotes the group ID of element . The cost of a group is equal to the sum of all elements in that belong to that group.
$$\text{cost}(x) = \sum_{i \text{ s.t. } G_{i} = x} A_{i} $$Two different group IDs, and (where ), are adjacent if and only if is either or for every . Finally, the value of two group IDs and is defined as the absolute difference between and .
$$\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 value between any pair of adjacent group IDs is maximized; you only need to output the largest value.
For example, let . There are ways to assign each element in into a group in this example; some of them are shown as follows.
- . There are pairs of group IDs that are adjacent and their 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 value in this group assignment is .
- . There are pairs of group IDs that are adjacent and their 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 value in this group assignment is .
The other group assignments are: , , , , , and . Among all possible group assignments in this example, the maximum largest that can be obtained is from the group assignment .
输入格式
Input begins with a line containing an integer () representing the number of elements in array . The next line contains integers () representing the array .
输出格式
Output contains an integer in a line representing the maximum possible largest 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 of can be obtained from the group assignment . The value of the only adjacent group IDs is: .
Explanation for the sample input/output #3
The maximum possible largest of can be obtained from the group assignment . The values of any two adjacent group IDs are: , , and .