CF1651C.Fault-tolerant Network

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 have grades a1,a2,…,ana_1, a_2, \dots, a_n and in the second row — b1,b2,…,bnb_1, b_2, \dots, b_n.

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 \le i \lt n), so two rows form two independent computer networks.

Your task is to combine them into 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∣|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 computers failing. In other words, if one computer is broken (no matter which one), the network won't split into two or more parts.

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

教室中有两排计算机。每排有 nn 台计算机,每台计算机都有自己的分数。第一排计算机的分数为 a1,a2,…,ana_1, a_2, \dots, a_n,第二排计算机的分数为 b1,b2,…,bnb_1, b_2, \dots, b_n。

初始时,每排中所有相邻的计算机之间都用网线连接(即对所有满足 1≤i<n1 \le i \lt n 的 ii,连接第 ii 台与第 i+1i+1 台计算机),因此这两排各自构成两个独立的计算机网络。

你的任务是通过连接若干对来自不同排的计算机,将这两个网络合并为一个统一的网络。连接第一排第 ii 台计算机与第二排第 jj 台计算机的代价为 ∣ai−bj∣|a_i - b_j|。

一台计算机可以与多台其他计算机相连,但你必须提供至少基本的容错能力:即需以某种方式连接计算机,使得当其中任意一台计算机发生故障时,整个网络仍保持连通(即不会分裂成两个或更多互不连通的部分)。

构造一个具备容错能力的网络所需的最小总代价是多少?

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — 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 \le n \le 2 \cdot 10^5) — the number of computers in each row.

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1091 \le a_i \le 10^9) — the grades of computers in the first row.

The third line contains nn integers b1,b2,…,bnb_1, b_2, \dots, b_n (1≤bi≤1091 \le b_i \le 10^9) — the grades of computers in the second row.

It's guaranteed that the total sum of nn doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。接下来是 tt 个测试用例。

每个测试用例的第一行包含一个整数 nn(3≤n≤2⋅1053 \le n \le 2 \cdot 10^5)—— 每行计算机的数量。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1091 \le a_i \le 10^9)—— 第一行中各计算机的评分。

每个测试用例的第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \dots, b_n(1≤bi≤1091 \le b_i \le 10^9)—— 第二行中各计算机的评分。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \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 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.

在第一个测试用例中,最优方案是连接四对计算机:

  1. 第一行的计算机 11 与第二行的计算机 22:代价为 ∣1−4∣=3|1 - 4| = 3;
  2. 第一行的计算机 33 与第二行的计算机 22:代价为 ∣1−4∣=3|1 - 4| = 3;
  3. 第一行的计算机 22 与第二行的计算机 11:代价为 ∣10−20∣=10|10 - 20| = 10;
  4. 第一行的计算机 22 与第二行的计算机 33:代价为 ∣10−25∣=15|10 - 25| = 15;

总计为 3+3+10+15=313 + 3 + 10 + 15 = 31。

在第二个测试用例中,最优方案是连接第一行的计算机 11 与第二行的计算机 11,以及第一行的计算机 44 与第二行的计算机 44。

输入解题思路,AI测评打分。不知道怎么写?

首页