CF1898D.Absolute Beauty

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Kirill has two integer arrays a1,a2,…,ana_1,a_2,\ldots,a_n and b1,b2,…,bnb_1,b_2,\ldots,b_n of length nn. He defines the absolute beauty of the array bb as $$\sum_{i=1}^{n} |a_i - b_i|.$$ Here, ∣x∣|x| denotes the absolute value of xx.

Kirill can perform the following operation at most once:

  • select two indices ii and jj (1≤i<j≤n1 \leq i \lt j \leq n) and swap the values of bib_i and bjb_j.

Help him find the maximum possible absolute beauty of the array bb after performing at most one swap.

基里尔有两个长度为 nn 的整数数组 a1,a2,…,ana_1,a_2,\ldots,a_n 和 b1,b2,…,bnb_1,b_2,\ldots,b_n。他将数组 bb 的绝对美观度定义为

∑i=1n∣ai−bi∣.\sum_{i=1}^{n} |a_i - b_i|.

其中,∣x∣|x| 表示 xx 的绝对值。

基里尔最多可以执行以下操作一次:

  • 选择两个下标 ii 和 jj(满足 1≤i<j≤n1 \leq i < j \leq n),并交换 bib_i 与 bjb_j 的值。

请帮助他求出:在最多执行一次交换操作后,数组 bb 能达到的最大绝对美观度。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10 0001 \leq t \leq 10\,000). The description of test cases follows.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052\leq n\leq 2\cdot 10^5) — the length of the arrays aa and bb.

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

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

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤10 0001 \leq t \leq 10\,000)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052\leq n\leq 2\cdot 10^5)—— 数组 aa 和 bb 的长度。

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

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

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, output one integer — the maximum possible absolute beauty of the array bb after no more than one swap.

对于每个测试用例,输出一个整数——在最多执行一次交换操作后,数组 bb 的最大可能绝对美丽值。

输入输出样例

  • 输入#1

    6
    3
    1 3 5
    3 3 3
    2
    1 2
    1 2
    2
    1 2
    2 1
    4
    1 2 3 4
    5 6 7 8
    10
    1 8 2 5 3 5 3 1 1 3
    2 9 2 4 8 2 3 5 3 1
    3
    47326 6958 358653
    3587 35863 59474

    输出#1

    4
    2
    2
    16
    31
    419045

说明/提示

In the first test case, each of the possible swaps does not change the array bb.

In the second test case, the absolute beauty of the array bb without performing the swap is ∣1−1∣+∣2−2∣=0|1-1| + |2-2| = 0. After swapping the first and the second element in the array bb, the absolute beauty becomes ∣1−2∣+∣2−1∣=2|1-2| + |2-1| = 2. These are all the possible outcomes, hence the answer is 22.

In the third test case, it is optimal for Kirill to not perform the swap. Similarly to the previous test case, the answer is 22.

In the fourth test case, no matter what Kirill does, the absolute beauty of bb remains equal to 1616.

在第一个测试用例中,所有可能的交换操作均不会改变数组 bb。

在第二个测试用例中,不执行交换操作时,数组 bb 的绝对美观度为 ∣1−1∣+∣2−2∣=0|1-1| + |2-2| = 0。若交换数组 bb 中的第一个与第二个元素,则绝对美观度变为 ∣1−2∣+∣2−1∣=2|1-2| + |2-1| = 2。这些是所有可能的结果,因此答案为 22。

在第三个测试用例中,Kirill 最优的选择是不执行交换操作。与上一个测试用例类似,答案为 22。

在第四个测试用例中,无论 Kirill 执行何种操作,bb 的绝对美观度始终为 1616。

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

首页