D. Absolute Beauty

D. Absolute Beauty

Kirill has two integer arrays a1,a2,,an and b1,b2,,bn of length n. He defines the absolute beauty of the array b as i=1n|aibi|. Here, |x| denotes the absolute value of x.

Kirill can perform the following operation at most once:

  • select two indices i and j (1i<jn) and swap the values of bi and bj.

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 (1t10000). The description of test cases follows.

The first line of each test case contains a single integer n (2n2105) — the length of the arrays a and b.

The second line of each test case contains n integers a1,a2,,an (1ai109) — the array a.

The third line of each test case contains n integers b1,b2,,bn (1bi109) — the array b.

It is guaranteed that the sum of n over all test cases does not exceed 2105.

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 |11|+|22|=0. After swapping the first and the second element in the array b, the absolute beauty becomes |12|+|21|=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 掉大分。一开始还以为是推式子然后用数据结构维护枚举统计最大值,然后直接司马了。直接抄的官方题解。

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

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

  可以知道只有在第一种情况线段的长度会增加,对此我们希望第一个区间的右端点离第二个区间的左端点尽可能远。因此考虑所有的 ijij,找到 ajbi 的最大值,那么答案就是初始时所有线段的总长度加上 2max{0,ajbi}。为了快速得到答案,只需求出 ai 的最大值,bi 的最小值,把差值的两倍加上即可(差值要大于 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 @   onlyblues  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2022-11-21 B. Elimination of a Ring
Web Analytics
点击右上角即可分享
微信分享提示