A14909 | Fault-tolerant Network
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There is a classroom with two rows of computers. There are $n$ computers in each row and each computer has its own grade. Computers in the first row has grades $a_1, a_2, \dots, a_n$ and in the second row — $b_1, b_2, \dots, b_n$ .
Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le 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 $i$ -th computer from the first row and the $j$ -th computer from the second row costs $|a_i - b_j|$ .
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?
Initially, all pairs of neighboring computers in each row are connected by wire (pairs $(i, i + 1)$ for all $1 \le 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 $i$ -th computer from the first row and the $j$ -th computer from the second row costs $|a_i - b_j|$ .
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?
输入格式
The first line contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of test cases. Next $t$ cases follow.
The first line of each test case contains the single integer $n$ ( $3 \le n \le 2 \cdot 10^5$ ) — the number of computers in each row.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le 10^9$ ) — the grades of computers in the first row.
The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ( $1 \le b_i \le 10^9$ ) — the grades of computers in the second row.
It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$ .
The first line of each test case contains the single integer $n$ ( $3 \le n \le 2 \cdot 10^5$ ) — the number of computers in each row.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le 10^9$ ) — the grades of computers in the first row.
The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ( $1 \le b_i \le 10^9$ ) — the grades of computers in the second row.
It's guaranteed that the total sum of $n$ doesn't exceed $2 \cdot 10^5$ .
输出格式
For each test case, print a single integer — the minimum total cost to make a fault-tolerant network.
输入输出样例
输入 #1
2 3 1 10 1 20 4 25 4 1 1 1 1 1000000000 1000000000 1000000000 1000000000
输出 #1
31 1999999998
In the first test case, it's optimal to connect four pairs of computers:
1. computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$ ;
2. computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$ ;
3. computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$ ;
4. computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$ ;
In total, $3 + 3 + 10 + 15 = 31$ .In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
1. computer $1$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$ ;
2. computer $3$ from the first row with computer $2$ from the second row: cost $|1 - 4| = 3$ ;
3. computer $2$ from the first row with computer $1$ from the second row: cost $|10 - 20| = 10$ ;
4. computer $2$ from the first row with computer $3$ from the second row: cost $|10 - 25| = 15$ ;
In total, $3 + 3 + 10 + 15 = 31$ .In the second test case, it's optimal to connect $1$ from the first row with $1$ from the second row, and $4$ from the first row with $4$ from the second row.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted