A15161 | Tokitsukaze and Two Colorful Tapes
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Tokitsukaze has two colorful tapes. There are $n$ distinct colors, numbered $1$ through $n$ , and each color appears exactly once on each of the two tapes. Denote the color of the $i$ -th position of the first tape as $ca_i$ , and the color of the $i$ -th position of the second tape as $cb_i$ .
Now Tokitsukaze wants to select each color an integer value from $1$ to $n$ , 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 $i$ -th position of the first tape as $numa_i$ , and the number of the $i$ -th position of the second tape as $numb_i$ .
For example, for the above picture, assuming that the color red has value $x$ ( $1 \leq x \leq n$ ), it appears at the $1$ -st position of the first tape and the $3$ -rd position of the second tape, so $numa_1=numb_3=x$ .
Note that each color $i$ from $1$ to $n$ 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.
Now Tokitsukaze wants to select each color an integer value from $1$ to $n$ , 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 $i$ -th position of the first tape as $numa_i$ , and the number of the $i$ -th position of the second tape as $numb_i$ .
For example, for the above picture, assuming that the color red has value $x$ ( $1 \leq x \leq n$ ), it appears at the $1$ -st position of the first tape and the $3$ -rd position of the second tape, so $numa_1=numb_3=x$ .
Note that each color $i$ from $1$ to $n$ 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.
输入格式
The first contains a single positive integer $t$ ( $1 \leq t \leq 10^4$ ) — the number of test cases.
For each test case, the first line contains a single integer $n$ ( $1\leq n \leq 10^5$ ) — the number of colors.
The second line contains $n$ integers $ca_1, ca_2, \ldots, ca_n$ ( $1 \leq ca_i \leq n$ ) — the color of each position of the first tape. It is guaranteed that $ca$ is a permutation.
The third line contains $n$ integers $cb_1, cb_2, \ldots, cb_n$ ( $1 \leq cb_i \leq n$ ) — the color of each position of the second tape. It is guaranteed that $cb$ is a permutation.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^{5}$ .
For each test case, the first line contains a single integer $n$ ( $1\leq n \leq 10^5$ ) — the number of colors.
The second line contains $n$ integers $ca_1, ca_2, \ldots, ca_n$ ( $1 \leq ca_i \leq n$ ) — the color of each position of the first tape. It is guaranteed that $ca$ is a permutation.
The third line contains $n$ integers $cb_1, cb_2, \ldots, cb_n$ ( $1 \leq cb_i \leq n$ ) — the color of each position of the second tape. It is guaranteed that $cb$ is a permutation.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \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 $\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 $\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$ .
The beauty is $\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 $\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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted