A16111 | Maximum Monogonosity
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given an array $a$ of length $n$ and an array $b$ of length $n$ . The cost of a segment $[l, r]$ , $1 \le l \le r \le n$ , is defined as $|b_l - a_r| + |b_r - a_l|$ .
Recall that two segments $[l_1, r_1]$ , $1 \le l_1 \le r_1 \le n$ , and $[l_2, r_2]$ , $1 \le l_2 \le r_2 \le n$ , are non-intersecting if one of the following conditions is satisfied: $r_1 < l_2$ or $r_2 < l_1$ .
The length of a segment $[l, r]$ , $1 \le l \le r \le n$ , is defined as $r - l + 1$ .
Find the maximum possible sum of costs of non-intersecting segments $[l_j, r_j]$ , $1 \le l_j \le r_j \le n$ , whose total length is equal to $k$ .
Recall that two segments $[l_1, r_1]$ , $1 \le l_1 \le r_1 \le n$ , and $[l_2, r_2]$ , $1 \le l_2 \le r_2 \le n$ , are non-intersecting if one of the following conditions is satisfied: $r_1 < l_2$ or $r_2 < l_1$ .
The length of a segment $[l, r]$ , $1 \le l \le r \le n$ , is defined as $r - l + 1$ .
Find the maximum possible sum of costs of non-intersecting segments $[l_j, r_j]$ , $1 \le l_j \le r_j \le n$ , whose total length is equal to $k$ .
输入格式
Each test consists of multiple test cases. The first line contains a single integer $t$ $(1 \le t \le 1000)$ — the number of sets of input data. The description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ( $1 \le k \le n \le 3000$ ) — the length of array $a$ and the total length of segments.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $-10^9 \le a_i \le 10^9$ ) — the elements of array $a$ .
The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ( $-10^9 \le b_i \le 10^9$ ) — the elements of array $b$ .
It is guaranteed that the sum of $n$ over all test case does not exceed $3000$ .
The first line of each test case contains two integers $n$ and $k$ ( $1 \le k \le n \le 3000$ ) — the length of array $a$ and the total length of segments.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $-10^9 \le a_i \le 10^9$ ) — the elements of array $a$ .
The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ( $-10^9 \le b_i \le 10^9$ ) — the elements of array $b$ .
It is guaranteed that the sum of $n$ over all test case does not exceed $3000$ .
输出格式
For each test case, output a single number — the maximum possible sum of costs of such segments.
输入输出样例
输入 #1
5 4 4 1 1 1 1 1 1 1 1 3 2 1 3 2 5 2 3 5 1 1 2 3 4 5 1 2 3 4 5 7 2 1 3 7 6 4 7 2 1 5 3 2 7 4 5 4 2 17 3 5 8 16 2 5 9
输出 #1
0 10 0 16 28
In the first test case, the cost of any segment is $0$ , so the total cost is $0$ .
In the second test case, we can take the segment $[1, 1]$ with a cost of $8$ and the segment $[3, 3]$ with a cost of $2$ to get a total sum of $10$ . It can be shown that this is the optimal solution.
In the third test case, we are only interested in segments of length $1$ , and the cost of any such segment is $0$ .
In the fourth test case, it can be shown that the optimal sum is $16$ . For example, we can take the segments $[3, 3]$ and $[4, 4]$ .
In the second test case, we can take the segment $[1, 1]$ with a cost of $8$ and the segment $[3, 3]$ with a cost of $2$ to get a total sum of $10$ . It can be shown that this is the optimal solution.
In the third test case, we are only interested in segments of length $1$ , and the cost of any such segment is $0$ .
In the fourth test case, it can be shown that the optimal sum is $16$ . For example, we can take the segments $[3, 3]$ and $[4, 4]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted