A15290 | Mainak and Array
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Mainak has an array $a_1, a_2, \ldots, a_n$ 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 \le l \le r \le n$ , and any positive integer $k$ .
- Repeat this $k$ times: set $a_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 $(a_n - a_1)$ after exactly one such operation. Determine the maximum value of $(a_n - a_1)$ that he can obtain.
- 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 \le l \le r \le n$ , and any positive integer $k$ .
- Repeat this $k$ times: set $a_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 $(a_n - a_1)$ after exactly one such operation. Determine the maximum value of $(a_n - a_1)$ that he can obtain.
输入格式
Each test contains multiple test cases. The first line contains a single integer $t$ ( $1 \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 $n$ ( $1 \le n \le 2000$ ).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \le a_i \le 999$ ).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ .
The first line of each test case contains a single integer $n$ ( $1 \le n \le 2000$ ).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \le a_i \le 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 $(a_n - a_1)$ 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$ .</li><li> 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$ .</li><li> 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$$$.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted