CF1660D.Maximum Product Strikes Back
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an array a consisting of n integers. For each i (1≤i≤n) the following inequality is true: −2≤ai≤2.
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.
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.
The product of elements of an empty array (array of length 0) should be assumed to be 1.
给你一个由 n 个整数组成的数组 a。对每个 i(1≤i≤n),均满足不等式:−2≤ai≤2。
你可以从数组开头移除任意数量(可能为 0)的元素,也可以从数组末尾移除任意数量(可能为 0)的元素。你甚至可以将整个数组全部删除。
你需要回答以下问题:应从数组开头移除多少个元素、从数组末尾移除多少个元素,才能使剩余数组中所有元素的乘积(即各元素相乘的结果)达到最大?如果存在多种方式能得到最大乘积的数组,你可以输出其中任意一种方案。
空数组(长度为 0 的数组)的元素乘积定义为 1。
输入格式
The first line of input data contains an integer t (1≤t≤104) —the number of test cases in the test.
Then the descriptions of the input test cases follow.
The first line of each test case description contains an integer n (1≤n≤2⋅105) —the length of array a.
The next line contains n integers a1,a2,…,an (∣ai∣≤2) — elements of array a.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
输入数据的第一行包含一个整数 t(1≤t≤104)——测试用例的数量。
随后是各测试用例的描述。
每个测试用例描述的第一行包含一个整数 n(1≤n≤2⋅105)——数组 a 的长度。
下一行包含 n 个整数 a1,a2,…,an(∣ai∣≤2)——数组 a 的元素。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, output two non-negative numbers x and y (0≤x+y≤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.
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.
对于每个测试用例,输出两个非负整数 x 和 y(满足 0≤x+y≤n),使得在从数组开头移除 x 个元素、从数组末尾移除 y 个元素后,剩余数组中所有数字的乘积(即乘法结果)最大。
若存在多种方案可得到最大乘积,则输出其中任意一种即可。规定空数组的乘积为 1。
输入输出样例
输入#1
5 4 1 2 -1 2 3 1 1 -2 5 2 0 -2 2 -1 3 -2 -1 -1 3 -1 -2 -2
输出#1
0 2 3 0 2 0 0 1 1 0
说明/提示
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".
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.
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)⋅2⋅(−1)=4. This value is the maximum possible value that can be obtained. Thus, for this case the answer is: "2 0".
第一种情况中,乘积的最大值为 2。因此,我们可以删除前三个元素(得到数组 [2]),或者删除最后两个元素和第一个元素(同样得到数组 [2]),或者删除最后两个元素(得到数组 [1,2])。因此,在第一种情况下,符合要求的答案有:“3 0”、“1 2” 或 “0 2”。
第二种情况中,乘积的最大值为 1。此时我们可以删除数组中的所有元素,因为空数组的乘积定义为 1。因此答案可以是 “3 0”,但也存在其他可能的答案。
第三种情况中,我们可以删除数组的前两个元素,得到数组:[−2,2,−1]。该数组元素的乘积为 (−2)⋅2⋅(−1)=4,这是所能得到的最大乘积值。因此,本情况的答案为:“2 0”。
输入解题思路,AI测评打分。不知道怎么写?