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:
- computer 11 from the first row with computer 22 from the second row: cost |1−4|=3|1−4|=3;
- computer 33 from the first row with computer 22 from the second row: cost |1−4|=3|1−4|=3;
- computer 22 from the first row with computer 11 from the second row: cost |10−20|=10|10−20|=10;
- 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;
}