codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

2229C2.md (2165B)


      1 Source: https://codeforces.com/problemset/problem/2229/C2
      2 
      3 C2. We Be Flipping (Hard Version)
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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.
     22 
     23 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):
     24 
     25 * select an index $i$ ($1 \le i \le n$) such that $a_i \gt 0$
     26 * then for each $j$ where $1 \le j \le i$ do $a_j := -a_j$.
     27 
     28 Output a valid sequence of operations of length at most $n$ which $\color{red}{\text{maximises}}$ the sum of $a$ at the end.
     29 
     30 Input
     31 
     32 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.
     33 
     34 The first line of each testcase contains an integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
     35 
     36 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$).
     37 
     38 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     39 
     40 Output
     41 
     42 For each testcase, output a single integer $k$ ($0 \le k \le n$) — the number of operations you will perform.
     43 
     44 Now output $k$ integers $b_1,\ldots,b_k$ where $b_i$ is the index you perform the $i$th operation on.
     45 
     46 After performing the operations the sum of $a$ should be maximal.
     47 
     48 Example
     49 
     50 Input
     51 
     52 ```
     53 5
     54 
     55 5
     56 
     57 -1 -2 -3 -5 -4
     58 
     59 4
     60 
     61 5 7 10 19
     62 
     63 5
     64 
     65 1 -3 2 -1 10
     66 
     67 4
     68 
     69 16 -13 -18 -16
     70 
     71 11
     72 
     73 2 -10 -11 3 -10 15 7 18 16 17 -9
     74 ```
     75 
     76 Output
     77 
     78 ```
     79 0
     80 
     81 0
     82 
     83 2
     84 1 3
     85 0
     86 
     87 6
     88 6 3 1 5 4 7
     89 ```
     90 
     91 Note
     92 
     93 In the first testcase, no operations are possible.
     94 
     95 In the second testcase, the sum is already maximal.
     96 
     97 In the third testcase, operations are made as follows:
     98 
     99 * $[1, -3, 2, -1, 10] \xrightarrow{i = 1} [\color{red}{-1}, -3, 2, -1, 10]$
    100 * $[-1, -3, 2, -1, 10] \xrightarrow{i = 3} [\color{red}{1, 3, -2}, -1, 10]$
    101 
    102 This has sum $11$, which can be proven to be maximal.