QOJ.ac

QOJ

時間限制: 3.0 s 記憶體限制: 1024 MB 總分: 100 可 Hack ✓

#14319. Square Permutation I

统计

给定两个长度为 $n$ 的排列 $p$ 和 $q$。保证 $n$ 是一个奇数。对于每个位置 $i \in [1, n]$,你必须恰好执行以下操作之一:

  • 不执行任何操作,代价为 $0$。
  • 花费 $x_i$ 的代价,将 $p_i$ 修改为 $p_i^2$ 或将 $q_i$ 修改为 $q_i^2$。
  • 花费 $y_i$ 的代价,同时将 $p_i$ 修改为 $p_i^2$ 且将 $q_i$ 修改为 $q_i^2$。

请计算使数组 $p$ 的中位数等于 $A$ 且数组 $q$ 的中位数等于 $B$ 所需的最小代价。如果无法做到,输出 $-1$。

输入格式

每个测试点包含多个测试用例。第一行包含测试用例的数量 $t$ ($1 \le t \le 10^4$)。接下来是测试用例的描述。

每个测试用例的第一行包含三个整数 $n, A, B$ ($1 \le n \le 10^5, 1 \le A, B \le n^2$)。

第二行包含 $n$ 个正整数,表示排列 $p$。

第三行包含 $n$ 个正整数,表示排列 $q$。

第四行包含 $n$ 个正整数,表示数组 $x$。

第五行包含 $n$ 个正整数,表示数组 $y$ ($1 \le x_i \le y_i \le 10^4$)。

保证所有测试用例中 $n$ 的总和不超过 $10^5$。

输出格式

对于每个测试用例,如果无法做到,输出 $-1$。

否则,输出最小代价。

每个输出必须独占一行。

样例

输入样例 1

3
7 6 7
7 3 5 6 2 4 1
1 6 3 4 2 7 5
5 1 1 4 4 1 1
6 4 6 6 6 1 1
9 5 5
1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 9
3 2 3
1 2 3
2 3 1
10000 1 1
10000 1 1

输出样例 1

7
0
10000

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.