CF1859E.Maximum Monogonosity

省选/NOI-

通过率:0%

时间限制:2.50s

内存限制:512MB

AC君温馨提醒

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

题目描述

You are given an array aa of length nn and an array bb of length nn. The cost of a segment [l,r][l, r], 1≤l≤r≤n1 \le l \le r \le n, is defined as ∣bl−ar∣+∣br−al∣|b_l - a_r| + |b_r - a_l|.

Recall that two segments [l1,r1][l_1, r_1], 1≤l1≤r1≤n1 \le l_1 \le r_1 \le n, and [l2,r2][l_2, r_2], 1≤l2≤r2≤n1 \le l_2 \le r_2 \le n, are non-intersecting if one of the following conditions is satisfied: r1<l2r_1 \lt l_2 or r2<l1r_2 \lt l_1.

The length of a segment [l,r][l, r], 1≤l≤r≤n1 \le l \le r \le n, is defined as r−l+1r - l + 1.

Find the maximum possible sum of costs of non-intersecting segments [lj,rj][l_j, r_j], 1≤lj≤rj≤n1 \le l_j \le r_j \le n, whose total length is equal to kk.

给你一个长度为 nn 的数组 aa 和一个长度为 nn 的数组 bb。区间 [l,r][l, r](其中 1≤l≤r≤n1 \le l \le r \le n)的代价定义为 ∣bl−ar∣+∣br−al∣|b_l - a_r| + |b_r - a_l|。

回忆一下:两个区间 [l1,r1][l_1, r_1](其中 1≤l1≤r1≤n1 \le l_1 \le r_1 \le n)和 [l2,r2][l_2, r_2](其中 1≤l2≤r2≤n1 \le l_2 \le r_2 \le n)被称为不相交,当且仅当满足以下条件之一:r1<l2r_1 \lt l_2 或 r2<l1r_2 \lt l_1。

区间 [l,r][l, r](其中 1≤l≤r≤n1 \le l \le r \le n)的长度定义为 r−l+1r - l + 1。

求若干个互不相交的区间 [lj,rj][l_j, r_j](其中 1≤lj≤rj≤n1 \le l_j \le r_j \le n)的最大可能总代价,要求这些区间的总长度恰好等于 kk。

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1000)(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 nn and kk (1≤k≤n≤30001 \le k \le n \le 3000) — the length of array aa and the total length of segments.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (−109≤ai≤109-10^9 \le a_i \le 10^9) — the elements of array aa.

The third line of each test case contains nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n (−109≤bi≤109-10^9 \le b_i \le 10^9) — the elements of array bb.

It is guaranteed that the sum of nn over all test case does not exceed 30003000.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 输入数据组数。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 kk(1≤k≤n≤30001 \le k \le n \le 3000)—— 数组 aa 的长度以及所有区段的总长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(−109≤ai≤109-10^9 \le a_i \le 10^9)—— 数组 aa 的元素。

每个测试用例的第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \ldots, b_n(−109≤bi≤109-10^9 \le b_i \le 10^9)—— 数组 bb 的元素。

保证所有测试用例中 nn 的总和不超过 30003000。

输出格式

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 00, so the total cost is 00.

In the second test case, we can take the segment [1,1][1, 1] with a cost of 88 and the segment [3,3][3, 3] with a cost of 22 to get a total sum of 1010. It can be shown that this is the optimal solution.

In the third test case, we are only interested in segments of length 11, and the cost of any such segment is 00.

In the fourth test case, it can be shown that the optimal sum is 1616. For example, we can take the segments [3,3][3, 3] and [4,4][4, 4].

在第一个测试用例中,任意区间的代价均为 00,因此总代价为 00。

在第二个测试用例中,我们可以选取区间 [1,1][1, 1](代价为 88)和区间 [3,3][3, 3](代价为 22),得到总代价 1010。可以证明这是最优解。

在第三个测试用例中,我们仅关注长度为 11 的区间,而任意此类区间的代价均为 00。

在第四个测试用例中,可以证明最优总代价为 1616。例如,我们可以选取区间 [3,3][3, 3] 和 [4,4][4, 4]。

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

首页