A16013 | Long Long
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Today Alex was brought array $a_1, a_2, \dots, a_n$ of length $n$ . He can apply as many operations as he wants (including zero operations) to change the array elements.
In $1$ operation Alex can choose any $l$ and $r$ such that $1 \leq l \leq r \leq n$ , and multiply all elements of the array from $l$ to $r$ inclusive by $-1$ . In other words, Alex can replace the subarray $[a_l, a_{l + 1}, \dots, a_r]$ by $[-a_l, -a_{l + 1}, \dots, -a_r]$ in $1$ operation.
For example, let $n = 5$ , the array is $[1, -2, 0, 3, -1]$ , $l = 2$ and $r = 4$ , then after the operation the array will be $[1, 2, 0, -3, -1]$ .
Alex is late for school, so you should help him find the maximum possible sum of numbers in the array, which can be obtained by making any number of operations, as well as the minimum number of operations that must be done for this.
In $1$ operation Alex can choose any $l$ and $r$ such that $1 \leq l \leq r \leq n$ , and multiply all elements of the array from $l$ to $r$ inclusive by $-1$ . In other words, Alex can replace the subarray $[a_l, a_{l + 1}, \dots, a_r]$ by $[-a_l, -a_{l + 1}, \dots, -a_r]$ in $1$ operation.
For example, let $n = 5$ , the array is $[1, -2, 0, 3, -1]$ , $l = 2$ and $r = 4$ , then after the operation the array will be $[1, 2, 0, -3, -1]$ .
Alex is late for school, so you should help him find the maximum possible sum of numbers in the array, which can be obtained by making any number of operations, as well as the minimum number of operations that must be done for this.
输入格式
The first line contains a single integer $t$ ( $1 \leq t \leq 10^4$ ) — number of test cases. Then the descriptions of the test cases follow.
The first line of each test case contains one integer $n$ ( $1 \leq n \leq 2 \cdot 10^5$ ) — length of the array.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $-10^9 \leq a_i \leq 10^9$ ) — elements of the array.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$ .
The first line of each test case contains one integer $n$ ( $1 \leq n \leq 2 \cdot 10^5$ ) — length of the array.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $-10^9 \leq a_i \leq 10^9$ ) — elements of the array.
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$ .
输出格式
For each test case output two space-separated numbers: the maximum possible sum of numbers in the array and the minimum number of operations to get this sum.
Pay attention that an answer may not fit in a standard integer type, so do not forget to use 64-bit integer type.
Pay attention that an answer may not fit in a standard integer type, so do not forget to use 64-bit integer type.
输入输出样例
输入 #1
5 6 -1 7 -4 -2 5 -8 8 -1 0 0 -2 1 0 -3 0 5 2 -1 0 -3 -7 5 0 -17 0 1 0 4 -1 0 -2 -1
输出 #1
27 3 7 2 13 1 18 1 4 1
Below, for each test case, only one of the possible shortest sequences of operations is provided among many. There are others that have the same length and lead to the maximum sum of elements.
In the first test case, Alex can make operations: $(1, 4)$ , $(2, 2)$ , $(6, 6)$ .
In the second test case, to get the largest sum you need to make operations: $(1, 8)$ , $(5, 6)$ .
In the fourth test case, it is necessary to make only one operation: $(2, 3)$ .
In the first test case, Alex can make operations: $(1, 4)$ , $(2, 2)$ , $(6, 6)$ .
In the second test case, to get the largest sum you need to make operations: $(1, 8)$ , $(5, 6)$ .
In the fourth test case, it is necessary to make only one operation: $(2, 3)$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted