#P11064. 【MX-X4-T4】「Jason-1」一步最优
【MX-X4-T4】「Jason-1」一步最优
Background
Original link: https://oier.team/problems/X4E。
Problem Description
For the following problem:
- You are given an array of length (where may be negative) and a positive integer .
- You need to choose at most index intervals of array , and ensure that they are pairwise disjoint.
- You need to maximize the total sum of the elements inside the chosen intervals, i.e., maximize .
Consider the following greedy algorithm (which is incorrect):
- Repeat the following process times:
- Among all intervals that do not intersect any currently chosen interval (including the empty interval), choose an interval with the maximum element sum.
- If there are multiple intervals with the maximum element sum, choose any one of them.
- Note: If the empty interval is chosen, it is equivalent to stopping the process, i.e., the number of chosen intervals is less than or equal to .
- Take the sum of the elements in all non-empty intervals chosen in the process above as the answer.
Now you are given and the array . For each , find the maximum possible value and the minimum possible value of the answer (i.e., the total of interval sums) that this algorithm may produce.
Input Format
This problem contains multiple test cases.
The first line contains a positive integer , the number of test cases. For each test case:
- The first line contains a positive integer .
- The second line contains integers .
Output Format
For each test case:
- The first line contains integers. The -th integer denotes the maximum value of the answer that the algorithm can obtain when .
- The second line contains integers. The -th integer denotes the minimum value of the answer that the algorithm can obtain when .
5
5
1 -1 1 -1 1
5
2 -2 2 -1 2
6
3 1 -4 1 -5 9
13
1 1 -4 5 -1 -4 1 9 1 9 -8 1 0
16
1 -8 2 -7 3 -6 4 -5 5 -4 4 -3 3 -2 2 -1
1 2 3 3 3
1 1 1 1 1
3 5 5 5 5
3 3 3 3 3
9 13 14 14 14 14
9 13 14 14 14 14
20 25 27 28 28 28 28 28 28 28 28 28 28
20 22 23 23 23 23 23 23 23 23 23 23 23
5 9 13 16 19 21 23 24 24 24 24 24 24 24 24 24
5 9 12 14 15 15 15 15 15 15 15 15 15 15 15 15
Hint
Sample Explanation
For the first test case:
- One way to achieve the maximum is to select intervals in order.
- One way to achieve the minimum is to select intervals $[1,5],\varnothing,\varnothing,\varnothing,\varnothing$ in order.
For the second test case:
- One way to achieve the maximum is to select intervals in order.
- One way to achieve the minimum is to select intervals $[1,5],\varnothing,\varnothing,\varnothing,\varnothing$ in order.
Note: In the first update, you cannot choose intervals like , because they do not satisfy that their interval sum is the maximum among all intervals in (i.e., ). In any update, you can never choose interval , because its interval sum is , and (which is the interval sum of the empty interval).
For the third test case, there is only one possible selection scheme, i.e., select intervals $[6,6],[1,2],[4,4],\varnothing,\varnothing,\varnothing$ in order.
Constraints
This problem uses bundled testdata.
| Subtask | Score | ||
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 |
For of the testdata, , , , and 。
Translated by ChatGPT 5