CF1726A.Mainak and Array

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Mainak has an array a1,a2,…,ana_1, a_2, \ldots, a_n of nn positive integers. He will do the following operation to this array exactly once:

  • Pick a subsegment of this array and cyclically rotate it by any amount.

Formally, he can do the following exactly once:

  • Pick two integers ll and rr, such that 1≤l≤r≤n1 \le l \le r \le n, and any positive integer kk.
  • Repeat this kk times: set al=al+1,al+1=al+2,…,ar−1=ar,ar=ala_l=a_{l+1}, a_{l+1}=a_{l+2}, \ldots, a_{r-1}=a_r, a_r=a_l (all changes happen at the same time).

Mainak wants to maximize the value of (an−a1)(a_n - a_1) after exactly one such operation. Determine the maximum value of (an−a1)(a_n - a_1) that he can obtain.

Mainak 有一个由 nn 个正整数组成的数组 a1,a2,…,ana_1, a_2, \ldots, a_n。他将对该数组恰好执行一次如下操作:

  • 选取该数组的一个子段,并将其进行任意次数的循环移位(即循环旋转)。

形式化地,他可以恰好执行一次以下操作:

  • 选取两个整数 ll 和 rr,满足 1≤l≤r≤n1 \le l \le r \le n,以及任一正整数 kk;
  • 重复以下步骤 kk 次:令 al=al+1,  al+1=al+2,  …,  ar−1=ar,  ar=ala_l=a_{l+1},\; a_{l+1}=a_{l+2},\; \ldots,\; a_{r-1}=a_r,\; a_r=a_l(所有赋值同时发生)。

Mainak 希望在恰好执行一次上述操作后,最大化 (an−a1)(a_n - a_1) 的值。请确定他所能得到的 (an−a1)(a_n - a_1) 的最大可能值。

输入格式

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤501 \le t \le 50) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤20001 \le n \le 2000).

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤9991 \le a_i \le 999).

It is guaranteed that the sum of nn over all test cases does not exceed 20002000.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤501 \le t \le 50),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤20001 \le n \le 2000)。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤9991 \le a_i \le 999)。

保证所有测试用例的 nn 值之和不超过 20002000。

输出格式

For each test case, output a single integer — the maximum value of (an−a1)(a_n - a_1) that Mainak can obtain by doing the operation exactly once.

对于每个测试用例,输出一个整数——Mainak 恰好执行一次该操作所能得到的 (an−a1)(a_n - a_1) 的最大值。

输入输出样例

  • 输入#1

    5
    6
    1 3 9 11 5 7
    1
    20
    3
    9 99 999
    4
    2 1 8 1
    3
    2 1 5

    输出#1

    10
    0
    990
    7
    4

说明/提示

  • In the first test case, we can rotate the subarray from index 33 to index 66 by an amount of 22 (i.e. choose l=3l = 3, r=6r = 6 and k=2k = 2) to get the optimal array: $$[1, 3, \underline{9, 11, 5, 7}] \longrightarrow [1, 3, \underline{5, 7, 9, 11}]$$ So the answer is an−a1=11−1=10a_n - a_1 = 11 - 1 = 10.

  • In the second testcase, it is optimal to rotate the subarray starting and ending at index 11 and rotating it by an amount of 22.

  • In the fourth testcase, it is optimal to rotate the subarray starting from index 11 to index 44 and rotating it by an amount of 33. So the answer is 8−1=78 - 1 = 7.

  • 在第一个测试用例中,我们可以将从索引 33 到索引 66 的子数组向右旋转 22 位(即选择 l=3l = 3、r=6r = 6 和 k=2k = 2),从而得到最优数组:

    \[1, 3, \\underline{9, 11, 5, 7}\] \\longrightarrow \[1, 3, \\underline{5, 7, 9, 11}\]

    因此答案为 an−a1=11−1=10a_n - a_1 = 11 - 1 = 10。

  • 在第二个测试用例中,最优策略是将起始和结束索引均为 11 的子数组向右旋转 22 位。

  • 在第四个测试用例中,最优策略是将从索引 11 到索引 44 的子数组向右旋转 33 位。因此答案为 8−1=78 - 1 = 7。

输入解题思路,AI测评打分。不知道怎么写?

首页