Spectral::Cup 2026 Round 2 (Codeforces Round 1100, Div. 1 + Div. 2)
C2
我们来翻转(困难版)
We Be Flipping (Hard Version)
Codeforces
1400
原题
← C1D →

我们来翻转(困难版)AI (CI翻译)

This is the hard version of the problem. The difference between the versions is that in this version, you must maximise the sum. You can hack only if you solved all versions of this problem.

这是本题的困难版本。两个版本的区别在于,本版本要求最大化总和。只有解决了本题的所有版本后才能进行 hack。AI (CI翻译)

You have an array $a$ of length $n$ which consists of non-zero (but possibly negative) integers. You will perform the following operation at most $n$ times (possibly none):

给定一个长度为 $n$ 的数组 $a$,其中的元素均为非零整数(但可能为负数)。你至多可以执行 $n$ 次以下操作(也可以一次都不执行):AI (CI翻译)

  • select an index $i$ ($1 \le i \le n$) such that $a_i \gt 0$
  • then for each $j$ where $1 \le j \le i$ do $a_j := -a_j$.
  • 选择一个下标 $i$($1 \le i \le n$),满足 $a_i \gt 0$;
  • 然后对于所有满足 $1 \le j \le i$ 的 $j$,执行 $a_j := -a_j$。
AI (CI翻译)

Output a valid sequence of operations of length at most $n$ which $\color{red}{\text{maximises}}$ the sum of $a$ at the end.

请输出一个长度至多为 $n$ 的合法操作序列,使得操作结束时 $a$ 的元素之和 $\color{red}{\text{最大化}}$。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

每个测试包含多个测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 10^4$)。接下来是各个测试用例的描述。AI (CI翻译)

The first line of each testcase contains an integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$.

每个测试用例的第一行包含一个整数 $n$($2 \le n \le 2 \cdot 10^5$)——数组 $a$ 的长度。AI (CI翻译)

The second line of each testcase contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-10^9 \le a_i \le 10^9, a_i \ne 0$).

每个测试用例的第二行包含 $n$ 个整数 $a_1,a_2,\ldots,a_n$($-10^9 \le a_i \le 10^9, a_i \ne 0$)。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

保证所有测试用例的 $n$ 之和不超过 $2 \cdot 10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each testcase, output a single integer $k$ ($0 \le k \le n$) — the number of operations you will perform.

对于每个测试用例,输出一个整数 $k$($0 \le k \le n$)——你将执行的操作次数。AI (CI翻译)

Now output $k$ integers $b_1,\ldots,b_k$ where $b_i$ is the index you perform the $i$th operation on.

接下来输出 $k$ 个整数 $b_1,\ldots,b_k$,其中 $b_i$ 是第 $i$ 次操作所选择的下标。AI (CI翻译)

After performing the operations the sum of $a$ should be maximal.

执行这些操作后,$a$ 的元素之和应为最大值。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first testcase, no operations are possible.

在第一个测试用例中,无法执行任何操作。AI (CI翻译)

In the second testcase, the sum is already maximal.

在第二个测试用例中,元素之和已经达到最大值。AI (CI翻译)

In the third testcase, operations are made as follows:

在第三个测试用例中,操作过程如下:AI (CI翻译)

  • $[1, -3, 2, -1, 10] \xrightarrow{i = 1} [\color{red}{-1}, -3, 2, -1, 10]$
  • $[-1, -3, 2, -1, 10] \xrightarrow{i = 3} [\color{red}{1, 3, -2}, -1, 10]$
  • $[1, -3, 2, -1, 10] \xrightarrow{i = 1} [\color{red}{-1}, -3, 2, -1, 10]$
  • $[-1, -3, 2, -1, 10] \xrightarrow{i = 3} [\color{red}{1, 3, -2}, -1, 10]$
AI (CI翻译)

This has sum $11$, which can be proven to be maximal.

此时元素之和为 $11$,可以证明这是可能达到的最大值。AI (CI翻译)

样例

Input #1
5
5
-1 -2 -3 -5 -4
4
5 7 10 19
5
1 -3 2 -1 10
4
16 -13 -18 -16
11
2 -10 -11 3 -10 15 7 18 16 17 -9
Output #1
0

0

2
1 3
0

6
6 3 1 5 4 7
InputCopy OutputCopy
题目信息
题号
2229C2
时间限制
2 s
内存限制
256 MB
标签
构造
动态规划
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。