AT_arc232_c.All Add or Single Negate
省选/NOI-
通过率:0%
时间限制:3.00s
内存限制:1024MB
AC君温馨提醒
该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given length-N integer sequences: A=(A1,A2,…,AN) and B=(B1,B2,…,BN).
You can perform the following two types of operations any number of times in any order.
- Add 1 to every element of A.
- Choose an integer i satisfying 1≤i≤N, and replace Ai with −Ai.
Determine whether it is possible to make A and B equal as multisets. If it is possible, also find the minimum number of operations required. Here, A and B are equal as multisets when, for every integer, the number of its occurrences in A equals the number of its occurrences in B.
You are given T test cases; find the answer for each of them.
给你两个长度为 N 的整数序列:A=(A1,A2,…,AN) 和 B=(B1,B2,…,BN)。
你可以以任意顺序、任意次数执行以下两种操作:
- 将 A 的每个元素加 1;
- 选择一个满足 1≤i≤N 的整数 i,并将 Ai 替换为 −Ai。
判断是否可能通过若干次操作使 A 与 B 作为多重集(multiset)相等。若可能,还请找出所需操作的最小次数。这里,当且仅当对任意整数,它在 A 中出现的次数等于其在 B 中出现的次数时,称 A 与 B 作为多重集相等。
你将得到 T 组测试用例;对每组用例,请给出答案。
输入格式
The input is given from Standard Input in the following format:
T
case1
case2
⋮
caseT
Each test case is given in the following format:
N
A1 A2 … AN
B1 B2 … BN
输入从标准输入中按以下格式给出:
T
case1
case2
⋮
caseT
每个测试用例按以下格式给出:
N
A1 A2 … AN
B1 B2 … BN
输出格式
Output T lines. The i-th line should contain -1 if it is impossible to make A and B equal as multisets in the i-th test case, and otherwise the minimum number of operations required.
输出 T 行。第 i 行应包含 -1(如果在第 i 个测试用例中无法使 A 和 B 成为相等的多重集),否则为所需的最少操作次数。
输入输出样例
输入#1
8 4 -3 -1 0 2 -2 1 2 3 2 0 0 0 1 1 0 0 1 0 3 1 3 1 2 1 3 -3 1 3 -1 -1 -1 1 1 1 6 -4 -2 -2 0 1 3 -3 -1 0 2 2 4
输出#1
2 -1 0 3 4 1 2 5
说明/提示
Sample 1 Explanation:
In the first test case, first negate A2, then add 1 to every element, which makes A=(−2,2,1,3). Now A and B are equal as multisets. They cannot be made equal with one operation, so the minimum number of operations required is 2.
In the second test case, no matter how you perform the operations, A cannot be made equal to B=(0,1) as multisets.
Constraints
- 1≤T≤104
- 1≤N≤100
- −109≤Ai,Bi≤109 (1≤i≤N)
- The sum of N3 over all test cases in a single input is at most 106.
- All input values are integers.
样例 1 解释:
在第一个测试用例中,首先对 A2 取相反数,然后将每个元素加 1,得到 A=(−2,2,1,3)。此时 A 与 B 作为多重集相等。无法仅通过一次操作使二者相等,因此所需的最少操作次数为 2。
在第二个测试用例中,无论以何种方式执行操作,A 都无法作为多重集变得与 B=(0,1) 相等。
约束条件
- 1≤T≤104
- 1≤N≤100
- −109≤Ai,Bi≤109 (1≤i≤N)
- 所有测试用例的 N3 之和在单次输入中至多为 106。
- 所有输入值均为整数。
输入解题思路,AI测评打分。不知道怎么写?