D. Absolute Beauty

D. Absolute Beauty

Kirill has two integer arrays $a_1,a_2,\ldots,a_n$ and $b_1,b_2,\ldots,b_n$ of length $n$. He defines the absolute beauty of the array $b$ as $$\sum_{i=1}^{n} |a_i - b_i|.$$ Here, $|x|$ denotes the absolute value of $x$.

Kirill can perform the following operation at most once:

  • select two indices $i$ and $j$ ($1 \leq i < j \leq n$) and swap the values of $b_i$ and $b_j$.

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

Input

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

The first line of each test case contains a single integer $n$ ($2\leq n\leq 2\cdot 10^5$) — the length of the arrays $a$ and $b$.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1\leq a_i\leq 10^9$) — the array $a$.

The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1\leq b_i\leq 10^9$) — the array $b$.

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

Output

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

Example

input

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

output

4
2
2
16
31
419045

Note

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

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

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

In the fourth test case, no matter what Kirill does, the absolute beauty of $b$ remains equal to $16$.

 

解题思路

  nmd 这场前面几题全是思维贪心题,直接爆 0 掉大分。一开始还以为是推式子然后用数据结构维护枚举统计最大值,然后直接司马了。直接抄的官方题解。

  将数对 $(a_i,b_i)$ 看作数轴上的线段,那么 $|a_i - b_i|$ 就是这条线段的长度。交换 $b_i$ 和 $b_j$ 可以分成以下三种情况(实际上不止,但线段长度的变化可以归为以下 $3$ 类):

  官方题解默认 $a_i \leq b_i$,实际上如果 $a_i > b_i$ 然后互相交换并不会影响答案,带几组数据套上面的图模拟下就知道了。 

  可以知道只有在第一种情况线段的长度会增加,对此我们希望第一个区间的右端点离第二个区间的左端点尽可能远。因此考虑所有的 $i$ 和 $j$,$i \ne j$,找到 $a_j - b_i$ 的最大值,那么答案就是初始时所有线段的总长度加上 $2 \cdot \max\{0,a_j - b_i\}$。为了快速得到答案,只需求出 $a_i$ 的最大值,$b_i$ 的最小值,把差值的两倍加上即可(差值要大于 $0$)。

  AC 代码如下:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2e5 + 10;

int a[N], b[N];

void solve() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", a + i);
    }
    for (int i = 0; i < n; i++) {
        scanf("%d", b + i);
    }
    LL s = 0;
    int mx = -2e9, mn = 2e9;
    for (int i = 0; i < n; i++) {
        if (a[i] > b[i]) swap(a[i], b[i]);    // 令a[i]为线段左端点,b[i]为线段右端点
        s += b[i] - a[i];
        mx = max(mx, a[i]);
        mn = min(mn, b[i]);
    }
    printf("%lld\n", s + max(0, mx - mn << 1));
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Editorial for Codeforces Round #910 (Div. 2):https://codeforces.com/blog/entry/120960

posted @ 2023-11-21 18:27  onlyblues  阅读(15)  评论(0编辑  收藏  举报
Web Analytics