CF1726A.Mainak and Array
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Mainak has an array a1,a2,…,an of n 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 l and r , such that 1≤l≤r≤n , and any positive integer k .
- Repeat this k times: set al=al+1,al+1=al+2,…,ar−1=ar,ar=al (all changes happen at the same time).
Mainak wants to maximize the value of (an−a1) after exactly one such operation. Determine the maximum value of (an−a1) that he can obtain.
输入格式
Each test contains multiple test cases. The first line contains a single integer t ( 1≤t≤50 ) — the number of test cases. Description of the test cases follows.
The first line of each test case contains a single integer n ( 1≤n≤2000 ).
The second line of each test case contains n integers a1,a2,…,an ( 1≤ai≤999 ).
It is guaranteed that the sum of n over all test cases does not exceed 2000 .
输出格式
For each test case, output a single integer — the maximum value of (an−a1) that Mainak can obtain by doing the operation exactly once.
输入输出样例
输入#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 3 to index 6 by an amount of 2 (i.e. choose l=3 , r=6 and k=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 a_n−a_1=11−1=10 .
- In the second testcase, it is optimal to rotate the subarray starting and ending at index 1 and rotating it by an amount of 2 .
- In the fourth testcase, it is optimal to rotate the subarray starting from index 1 to index 4 and rotating it by an amount of 3 . So the answer is 8−1=7$$.