luogu#P16372. [IATI 2026] Partition
[IATI 2026] Partition
Problem Description
It is often said that siblings tend to share things as equally as possible. Here, we consider a somewhat interesting situation involving two sisters -- Martina and eni. Martina has recently bought packs of candies, where the -th packs contains a positive number of candies for and the total number of candies is . The sisters need to divide the candies between each other. For them, the number of candies is not the only important thing, but also the number of packs! After many arguments, the sisters have agreed on the following procedure.
First, eni will order the packs in any way she wants. Formally, she will produce a permutation of them. Let us denote this permutation by indices , where $\{i_0, i_1, \dots, i_{N-1}\} = \{0, 1, \dots, N-1\}$. Then Martina gives the packs to \textbf{D}eni in that order until the total number of candies given becomes . After that, Martina takes the remaining packs for herself and then the packs are fully partitioned between them.
eni loves her sister and so she will try to minimize the absolute difference between the number of packs she receives and the number of packs her sister receives. Therefore, she wants to pick a permutation that minimizes this among all possible permutations of the packs. Write a program that will help her determine such a permutation of the packs of candies in scenarios. If there are multiple solutions, you should return any one of them.
Implementation details
You should implement a function :
std::vector<int> solve(std::vector<int> A)
- : vector of positive numbers, representing the number of candies in the packs bought by Martina.
This function is called times -- once for each scenario in а test case. For each scenario, it has to return a vector containing a permutation of the indices such that it minimizes the absolute difference between the number of packs in the resulting partition from the permutation.
Input Format
Input format:
- line : one positive number -- the number of scenarios;
- line : one positive number -- the number of packs in the -th scenario;
- line : -- the number of candies in the packs in the -th scenario.
Output Format
Output format:
- line : the numbers returned by the -th call to .
2
9
5 6 6 3 1 1 4 4 3
8
2 2 3 2 3 2 2 3
0 3 6 7 4 8 1 2 5
2 4 6 7 0 1 3 5
Hint
Sample 1 Explanation
The given output of the first sample is one of multiple possible solutions. The number of candies in the reordered packs are: . eni will receive the packs with indices , thus getting candies. Note that , so Martina can't stop earlier. Martina will take the remaining packs so the absolute difference between the number of packs in the partition is . It can be proven that this is optimal for the test.
Constraints
- where is the sum of all among the scenarios in a given test case.
- for all
Subtasks
| Subtask | Points | Required subtasks |
Other constraints | ||
|---|---|---|---|---|---|
| The sample test. | |||||
| All values appear an even number of times; for ; | |||||
| for | |||||
| for , where | |||||
| for | |||||
| ^ | |||||
The points for a given subtask are obtained only if all the tests for it and the required subtasks are successfully passed.