CF1806C.Sequence Master

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

For some positive integer mm, YunQian considers an array qq of 2m2m (possibly negative) integers good, if and only if for every possible subsequence of qq that has length mm, the product of the mm elements in the subsequence is equal to the sum of the mm elements that are not in the subsequence. Formally, let U=1,2,…,2mU={1,2,\ldots,2m}. For all sets S⊆US \subseteq U such that ∣S∣=m|S|=m, ∏i∈Sqi=∑i∈U∖Sqi\prod\limits_{i \in S} q_i = \sum\limits_{i \in U \setminus S} q_i.

Define the distance between two arrays aa and bb both of length kk to be ∑i=1k∣ai−bi∣\sum\limits_{i=1}^k|a_i-b_i|.

You are given a positive integer nn and an array pp of 2n2n integers.

Find the minimum distance between pp and qq over all good arrays qq of length 2n2n. It can be shown for all positive integers nn, at least one good array exists. Note that you are not required to construct the array qq that achieves this minimum distance.

对于某个正整数 mm,云茜称一个长度为 2m2m 的(可能含负数的)整数数组 qq 是“好的”,当且仅当:对 qq 的任意一个长度为 mm 的子序列,该子序列中 mm 个元素的乘积等于其余 mm 个不在该子序列中的元素之和。形式化地,令 U={1,2,…,2m}U = \{1,2,\ldots,2m\}。对所有满足 ∣S∣=m|S|=m 的集合 S⊆US \subseteq U,均有

∏i∈Sqi=∑i∈U∖Sqi.\prod\limits_{i \in S} q_i = \sum\limits_{i \in U \setminus S} q_i.

定义两个长度均为 kk 的数组 aa 和 bb 之间的距离为 ∑i=1k∣ai−bi∣\sum\limits_{i=1}^k|a_i-b_i|。

现给定一个正整数 nn 和一个长度为 2n2n 的整数数组 pp。

求 pp 与所有长度为 2n2n 的“好”数组 qq 之间的最小距离。可以证明:对任意正整数 nn,至少存在一个“好”数组。注意:你无需构造出达到该最小距离的数组 qq。

输入格式

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

The first line of each test case contains a single integer nn (1≤n≤2⋅1051\le n\le 2\cdot10^5).

The second line of each test case contains 2n2n integers p1,p2,…,p2np_1, p_2, \ldots, p_{2n} (∣pi∣≤109|p_i| \le 10^9).

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

第一行包含一个整数 tt(1≤t≤1041\le t\le 10^4)—— 表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051\le n\le 2\cdot10^5)。

每个测试用例的第二行包含 2n2n 个整数 p1,p2,…,p2np_1, p_2, \ldots, p_{2n}(∣pi∣≤109|p_i| \le 10^9)。

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

输出格式

For each test case, output the minimum distance between pp and a good qq.

对于每个测试用例,输出点 pp 与一个“好”点 qq 之间的最小距离。

输入输出样例

  • 输入#1

    4
    1
    6 9
    2
    1 2 2 1
    2
    -2 -2 2 2
    4
    -3 -2 -1 0 1 2 3 4

    输出#1

    3
    2
    5
    13

说明/提示

In the first test case, it is optimal to let q=[6,6]q=[6,6].

In the second test case, it is optimal to let q=[2,2,2,2]q=[2,2,2,2].

在第一个测试用例中,令 q=[6,6]q=[6,6] 是最优的。

在第二个测试用例中,令 q=[2,2,2,2]q=[2,2,2,2] 是最优的。

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

首页