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 D\textbf{D}eni. Martina has recently bought NN packs of candies, where the ii-th packs contains a positive number of candies AiA_i for 0iN10 \le i \le N-1 and the total number of candies is S=A0+A1++AN1S = A_0 + A_1 + \dots + A_{N-1}. 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, D\textbf{D}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 i0,i1,,iN1i_0, i_1, \dots, i_{N-1}, where $\{i_0, i_1, \dots, i_{N-1}\} = \{0, 1, \dots, N-1\}$. Then Martina gives the packs Ai0,Ai1,A_{i_0}, A_{i_1}, \dots to \textbf{D}eni in that order until the total number of candies given becomes S2\ge \frac{S}{2}. After that, Martina takes the remaining packs for herself and then the packs are fully partitioned between them.

D\textbf{D}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 partition\textbf{partition} that will help her determine such a permutation of the packs of candies in TT scenarios. If there are multiple solutions, you should return any one of them.

Implementation details

You should implement a function solve\texttt{solve}:

std::vector<int> solve(std::vector<int> A)
  • AA: vector of NN positive numbers, representing the number of candies in the packs bought by Martina.

This function is called TT times -- once for each scenario in а test case. For each scenario, it has to return a vector containing a permutation of the indices 0,1,,N10, 1, \dots, N-1 such that it minimizes the absolute difference between the number of packs in the resulting partition from the permutation.

Input Format

Input format:

  • line 11: one positive number TT -- the number of scenarios;
  • line 2+2×j2 + 2 \times j: one positive number NN -- the number of packs in the jj-th scenario;
  • line 3+2×j3 + 2 \times j: A0 A1  AN1A_0 \ A_1 \ \dots \ A_{N-1} -- the number of candies in the packs in the jj-th scenario.

Output Format

Output format:

  • line jj: the numbers returned by the jj-th call to solve\texttt{solve}.
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: 5,3,4,4,1,3,6,6,15, 3, 4, 4, 1, 3, 6, 6, 1. D\textbf{D}eni will receive the packs with indices 0,3,6,7,40, 3, 6, 7, 4, thus getting 5+3+4+4+1=175 + 3 + 4 + 4 + 1 = 17 candies. Note that 5+3+4+4=16<S2=16.55 + 3 + 4 + 4 = 16 < \frac{S}{2} = 16.5, 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 54=1\lvert 5 - 4 \rvert = 1. It can be proven that this is optimal for the test.

Constraints

  • 1N2×1061 \le N \le 2 \times 10^6
  • 1N1071 \le \sum N \le 10^7 where N\sum N is the sum of all NN among the scenarios in a given test case.
  • 1T5×1031 \le T \le 5 \times 10^3
  • 1Ai1091 \le A_i \le 10^9 for all 0iN10 \le i \le N-1

Subtasks

Subtask Points Required
subtasks
NN ΣN\Sigma N Other constraints
00 - - The sample test.
11 33 105\le 10^5 All values A0,A1,,AN1A_0, A_1, \dots, A_{N-1} appear an even number of times; Ai3×107A_i \le 3 \times 10^7 for 0iN10 \le i \le N-1; T=1T = 1
22 25\le 25 250\le 250 Ai=2iA_i = 2^i for 0iN10 \le i \le N-1
33 22 106\le 10^6 5×106\le 5 \times 10^6 Ai=2siA_i = 2^{s_i} for 0iN10 \le i \le N-1, where 0si250 \le s_i \le 25
44 55 - 2×106\le 2 \times 10^6 107\le 10^7 Ai=i+1A_i = i+1 for 0iN10 \le i \le N-1
55 44 7\le 7 3.5×104\le 3.5 \times 10^4 -
66 00 20\le 20 200\le 200 ^
77 1919 0,2,60, 2, 6 2×103\le 2 \times 10^3 104\le 10^4
88 2828 02,570-2, 5-7 105\le 10^5 5×105\le 5 \times 10^5
99 2929 070-7 2×106\le 2 \times 10^6 107\le 10^7

The points for a given subtask are obtained only if all the tests for it and the required subtasks are successfully passed.