C. Fault-tolerant Network-Educational Codeforces Round 124 (Rated for Div. 2)

​​​​​​Problem - 1651C - Codeforces

C. Fault-tolerant Network

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There is a classroom with two rows of computers. There are nn computers in each row and each computer has its own grade. Computers in the first row has grades a1,a2,…,ana1,a2,…,an and in the second row — b1,b2,…,bnb1,b2,…,bn.

Initially, all pairs of neighboring computers in each row are connected by wire (pairs (i,i+1)(i,i+1) for all 1≤i<n1≤i<n), so two rows form two independent computer networks.

Your task is to combine them in one common network by connecting one or more pairs of computers from different rows. Connecting the ii-th computer from the first row and the jj-th computer from the second row costs |ai−bj||ai−bj|.

You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computer failing. In other words, if one computer is broken (no matter which one), the network won't split in two or more parts.

That is the minimum total cost to make a fault-tolerant network?

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. Next tt cases follow.

The first line of each test case contains the single integer nn (3≤n≤2⋅1053≤n≤2⋅105) — the number of computers in each row.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the grades of computers in the first row.

The third line contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤1091≤bi≤109) — the grades of computers in the second row.

It's guaranteed that the total sum of nn doesn't exceed 2⋅1052⋅105.

Output

For each test case, print a single integer — the minimum total cost to make a fault-tolerant network.

Example

input

Copy

2
3
1 10 1
20 4 25
4
1 1 1 1
1000000000 1000000000 1000000000 1000000000

output

Copy

31
1999999998

Note

In the first test case, it's optimal to connect four pairs of computers:

  1. computer 11 from the first row with computer 22 from the second row: cost |1−4|=3|1−4|=3;
  2. computer 33 from the first row with computer 22 from the second row: cost |1−4|=3|1−4|=3;
  3. computer 22 from the first row with computer 11 from the second row: cost |10−20|=10|10−20|=10;
  4. computer 22 from the first row with computer 33 from the second row: cost |10−25|=15|10−25|=15;

In total, 3+3+10+15=313+3+10+15=31.

In the second test case, it's optimal to connect 11 from the first row with 11 from the second row, and 44 from the first row with 44 from the second row.

=========================================================================

错综复杂的连接关系不可能判断连通性,只会有特殊情况,考虑两边连接的情况,画图分析即可,要想保证断掉任意一个,全图连通性不变,必须四个角的每个点都和对面有连接才行。

我们求一下四个角连接时的最小值,顺便特判一下四个角中有互相连接的情况即可。

#include <bits/stdc++.h>
# define inf 0x7f7f7f7f7f7f7f7f
using namespace std;

typedef long long int ll;

ll a[200000+10];
ll b[200000+10];

int main()
{
    int  t;

    cin>>t;

    while(t--)
    {

        int n;

        cin>>n;

        for(int i=1;i<=n;i++)
            cin>>a[i];

        for(int i=1;i<=n;i++)
            cin>>b[i];

        ll sl=inf,sr=inf,xl=inf,xr=inf;

        for(int i=1;i<=n;i++)
        {
            sl=min(sl,abs(a[1]-b[i]));

            sr=min(sr,abs(a[n]-b[i]));

        }
        for(int i=1;i<=n;i++)
        {
            xl=min(xl,abs(b[1]-a[i]));

            xr=min(xr,abs(b[n]-a[i]));

        }

        ll ans=inf;

        //  sl=xl sl=xr sr=xl sr=xr

        ans=min(abs(a[1]-b[1])+sr+xr,ans);

        ans=min(abs(a[1]-b[n])+sr+xl,ans);

        ans=min(abs(a[n]-b[1])+sl+xr,ans);

        ans=min(abs(a[n]-b[n])+sl+xl,ans);


        ans=min(ans,abs(a[1]-b[1])+abs(a[n]-b[n]));

        ans=min(ans,abs(a[1]-b[n])+abs(a[n]-b[1]));

        ans=min(ans,xl+xr+sl+sr);

        cout<<ans<<endl;

    }
    return 0;
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值