codeforces-scrape

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

1660D.md (2927B)


      1 Source: https://codeforces.com/problemset/problem/1660/D
      2 
      3 D. Maximum Product Strikes Back
      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 You are given an array $a$ consisting of $n$ integers. For each $i$ ($1 \le i \le n$) the following inequality is true: $-2 \le a_i \le 2$.
     22 
     23 You can remove any number (possibly $0$) of elements from the beginning of the array and any number (possibly $0$) of elements from the end of the array. You are allowed to delete the whole array.
     24 
     25 You need to answer the question: how many elements should be removed from the beginning of the array, and how many elements should be removed from the end of the array, so that the result will be an array whose product (multiplication) of elements is maximal. If there is more than one way to get an array with the maximum product of elements on it, you are allowed to output any of them.
     26 
     27 The product of elements of an empty array (array of length $0$) should be assumed to be $1$.
     28 
     29 Input
     30 
     31 The first line of input data contains an integer $t$ ($1 \le t \le 10^4$) —the number of test cases in the test.
     32 
     33 Then the descriptions of the input test cases follow.
     34 
     35 The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) —the length of array $a$.
     36 
     37 The next line contains $n$ integers $a_1, a_2, \dots, a_n$ ($|a_i| \le 2$) — elements of array $a$.
     38 
     39 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     40 
     41 Output
     42 
     43 For each test case, output two non-negative numbers $x$ and $y$ ($0 \le x + y \le n$) — such that the product (multiplication) of the array numbers, after removing $x$ elements from the beginning and $y$ elements from the end, is maximal.
     44 
     45 If there is more than one way to get the maximal product, it is allowed to output any of them. Consider the product of numbers on empty array to be $1$.
     46 
     47 Example
     48 
     49 Input
     50 
     51 ```
     52 5
     53 
     54 4
     55 
     56 1 2 -1 2
     57 
     58 3
     59 
     60 1 1 -2
     61 
     62 5
     63 
     64 2 0 -2 2 -1
     65 
     66 3
     67 
     68 -2 -1 -1
     69 
     70 3
     71 
     72 -1 -2 -2
     73 ```
     74 
     75 Output
     76 
     77 ```
     78 0 2
     79 3 0
     80 2 0
     81 0 1
     82 1 0
     83 ```
     84 
     85 Note
     86 
     87 In the first case, the maximal value of the product is $2$. Thus, we can either delete the first three elements (obtain array $[2]$), or the last two and one first element (also obtain array $[2]$), or the last two elements (obtain array $[1, 2]$). Thus, in the first case, the answers fit: "3 0", or "1 2", or "0 2".
     88 
     89 In the second case, the maximum value of the product is $1$. Then we can remove all elements from the array because the value of the product on the empty array will be $1$. So the answer is "3 0", but there are other possible answers.
     90 
     91 In the third case, we can remove the first two elements of the array. Then we get the array: $[-2, 2, -1]$. The product of the elements of the resulting array is $(-2) \cdot 2 \cdot (-1) = 4$. This value is the maximum possible value that can be obtained. Thus, for this case the answer is: "2 0".