CF1677C.Tokitsukaze and Two Colorful Tapes

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Tokitsukaze has two colorful tapes. There are nn distinct colors, numbered 11 through nn, and each color appears exactly once on each of the two tapes. Denote the color of the ii-th position of the first tape as caica_i, and the color of the ii-th position of the second tape as cbicb_i.

Now Tokitsukaze wants to select each color an integer value from 11 to nn, distinct for all the colors. After that she will put down the color values in each colored position on the tapes. Denote the number of the ii-th position of the first tape as numainuma_i, and the number of the ii-th position of the second tape as numbinumb_i.

For example, for the above picture, assuming that the color red has value xx (1≤x≤n1 \leq x \leq n), it appears at the 11-st position of the first tape and the 33-rd position of the second tape, so numa1=numb3=xnuma_1=numb_3=x.

Note that each color ii from 11 to nn should have a distinct value, and the same color which appears in both tapes has the same value.

After labeling each color, the beauty of the two tapes is calculated as $$\sum_{i=1}^{n}|numa_i-numb_i|.$$

Please help Tokitsukaze to find the highest possible beauty.

Tokitsukaze 有两条彩色胶带。共有 nn 种互不相同的颜色,编号为 11 到 nn,且每种颜色在每条胶带上恰好出现一次。记第一条胶带上第 ii 个位置的颜色为 caica_i,第二条胶带上第 ii 个位置的颜色为 cbicb_i。

现在,Tokitsukaze 想要为每种颜色分配一个 11 到 nn 之间的整数值,且所有颜色的值互不相同。随后,她将把这些数值填入胶带上对应颜色的位置中。记第一条胶带上第 ii 个位置的数值为 numainuma_i,第二条胶带上第 ii 个位置的数值为 numbinumb_i。

例如,在上图中,假设红色对应值 xx(其中 1≤x≤n1 \leq x \leq n),而红色出现在第一条胶带的第 11 个位置和第二条胶带的第 33 个位置,则有 numa1=numb3=xnuma_1 = numb_3 = x。

注意:颜色 11 至 nn 的取值必须互不相同,且同一种颜色(在两条胶带上均出现)必须赋予相同的数值。

完成颜色赋值后,两条胶带的“美观度”定义为

∑i=1n∣numai−numbi∣。\sum_{i=1}^{n}|numa_i - numb_i|。

请帮助 Tokitsukaze 求出可能达到的最大美观度。

输入格式

The first contains a single positive integer tt (1≤t≤1041 \leq t \leq 10^4) — the number of test cases.

For each test case, the first line contains a single integer nn (1≤n≤1051\leq n \leq 10^5) — the number of colors.

The second line contains nn integers ca1,ca2,…,canca_1, ca_2, \ldots, ca_n (1≤cai≤n1 \leq ca_i \leq n) — the color of each position of the first tape. It is guaranteed that caca is a permutation.

The third line contains nn integers cb1,cb2,…,cbncb_1, cb_2, \ldots, cb_n (1≤cbi≤n1 \leq cb_i \leq n) — the color of each position of the second tape. It is guaranteed that cbcb is a permutation.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^{5}.

第一行包含一个正整数 tt(1≤t≤1041 \leq t \leq 10^4)——测试用例的数量。

对于每个测试用例,第一行包含一个整数 nn(1≤n≤1051\leq n \leq 10^5)——颜色的种类数。

第二行包含 nn 个整数 ca1,ca2,…,canca_1, ca_2, \ldots, ca_n(1≤cai≤n1 \leq ca_i \leq n)——第一条胶带每个位置的颜色。保证 caca 是一个排列。

第三行包含 nn 个整数 cb1,cb2,…,cbncb_1, cb_2, \ldots, cb_n(1≤cbi≤n1 \leq cb_i \leq n)——第二条胶带每个位置的颜色。保证 cbcb 是一个排列。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^{5}。

输出格式

For each test case, print a single integer — the highest possible beauty.

对于每个测试用例,输出一个整数——可能达到的最高美丽值。

输入输出样例

  • 输入#1

    3
    6
    1 5 4 3 2 6
    5 3 1 4 6 2
    6
    3 5 4 6 2 1
    3 6 4 5 2 1
    1
    1
    1

    输出#1

    18
    10
    0

说明/提示

An optimal solution for the first test case is shown in the following figure:

The beauty is ∣4−3∣+∣3−5∣+∣2−4∣+∣5−2∣+∣1−6∣+∣6−1∣=18\left|4-3 \right|+\left|3-5 \right|+\left|2-4 \right|+\left|5-2 \right|+\left|1-6 \right|+\left|6-1 \right|=18.

An optimal solution for the second test case is shown in the following figure:

The beauty is ∣2−2∣+∣1−6∣+∣3−3∣+∣6−1∣+∣4−4∣+∣5−5∣=10\left|2-2 \right|+\left|1-6 \right|+\left|3-3 \right|+\left|6-1 \right|+\left|4-4 \right|+\left|5-5 \right|=10.

第一个测试用例的最优解如下图所示:

其美观度为 ∣4−3∣+∣3−5∣+∣2−4∣+∣5−2∣+∣1−6∣+∣6−1∣=18\left|4-3 \right|+\left|3-5 \right|+\left|2-4 \right|+\left|5-2 \right|+\left|1-6 \right|+\left|6-1 \right|=18。

第二个测试用例的最优解如下图所示:

其美观度为 ∣2−2∣+∣1−6∣+∣3−3∣+∣6−1∣+∣4−4∣+∣5−5∣=10\left|2-2 \right|+\left|1-6 \right|+\left|3-3 \right|+\left|6-1 \right|+\left|4-4 \right|+\left|5-5 \right|=10。

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

首页