CF1637D.Yet Another Minimization Problem

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given two arrays aa and bb, both of length nn.

You can perform the following operation any number of times (possibly zero): select an index ii (1≤i≤n1 \leq i \leq n) and swap aia_i and bib_i.

Let's define the cost of the array aa as ∑i=1n∑j=i+1n(ai+aj)2\sum_{i=1}^{n} \sum_{j=i + 1}^{n} (a_i + a_j)^2. Similarly, the cost of the array bb is ∑i=1n∑j=i+1n(bi+bj)2\sum_{i=1}^{n} \sum_{j=i + 1}^{n} (b_i + b_j)^2.

Your task is to minimize the total cost of two arrays.

给你两个长度均为 nn 的数组 aa 和 bb。

你可以执行以下操作任意次(也可以不执行):选择一个下标 ii(1≤i≤n1 \leq i \leq n),并交换 aia_i 与 bib_i。

定义数组 aa 的代价为 ∑i=1n∑j=i+1n(ai+aj)2\sum_{i=1}^{n} \sum_{j=i + 1}^{n} (a_i + a_j)^2。类似地,数组 bb 的代价为 ∑i=1n∑j=i+1n(bi+bj)2\sum_{i=1}^{n} \sum_{j=i + 1}^{n} (b_i + b_j)^2。

你的任务是最小化两个数组的总代价。

输入格式

Each test case consists of several test cases. The first line contains a single integer tt (1≤t≤401 \leq t \leq 40) — the number of test cases. The following is a description of the input data sets.

The first line of each test case contains an integer nn (1≤n≤1001 \leq n \leq 100) — the length of both arrays.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1001 \leq a_i \leq 100) — elements of the first array.

The third line of each test case contains nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n (1≤bi≤1001 \leq b_i \leq 100) — elements of the second array.

It is guaranteed that the sum of nn over all test cases does not exceed 100100.

每个测试用例包含若干个测试数据。第一行包含一个整数 tt(1≤t≤401 \leq t \leq 40),表示测试用例的数量。接下来是各组输入数据的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1001 \leq n \leq 100),表示两个数组的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1001 \leq a_i \leq 100),表示第一个数组的元素。

每个测试用例的第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \ldots, b_n(1≤bi≤1001 \leq b_i \leq 100),表示第二个数组的元素。

保证所有测试用例中 nn 的总和不超过 100100。

输出格式

For each test case, print the minimum possible total cost.

对于每个测试用例,输出最小的可能总成本。

输入输出样例

  • 输入#1

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

    输出#1

    0
    987
    914

说明/提示

In the second test case, in one of the optimal answers after all operations a=[2,6,4,6]a = [2, 6, 4, 6], b=[3,7,6,1]b = [3, 7, 6, 1].

The cost of the array aa equals to (2+6)2+(2+4)2+(2+6)2+(6+4)2+(6+6)2+(4+6)2=508(2 + 6)^2 + (2 + 4)^2 + (2 + 6)^2 + (6 + 4)^2 + (6 + 6)^2 + (4 + 6)^2 = 508.

The cost of the array bb equals to (3+7)2+(3+6)2+(3+1)2+(7+6)2+(7+1)2+(6+1)2=479(3 + 7)^2 + (3 + 6)^2 + (3 + 1)^2 + (7 + 6)^2 + (7 + 1)^2 + (6 + 1)^2 = 479.

The total cost of two arrays equals to 508+479=987508 + 479 = 987.

在第二个测试用例中,所有操作完成后的一个最优答案为 a=[2,6,4,6]a = [2, 6, 4, 6],b=[3,7,6,1]b = [3, 7, 6, 1]。

数组 aa 的代价为 (2+6)2+(2+4)2+(2+6)2+(6+4)2+(6+6)2+(4+6)2=508(2 + 6)^2 + (2 + 4)^2 + (2 + 6)^2 + (6 + 4)^2 + (6 + 6)^2 + (4 + 6)^2 = 508。

数组 bb 的代价为 (3+7)2+(3+6)2+(3+1)2+(7+6)2+(7+1)2+(6+1)2=479(3 + 7)^2 + (3 + 6)^2 + (3 + 1)^2 + (7 + 6)^2 + (7 + 1)^2 + (6 + 1)^2 = 479。

两个数组的总代价为 508+479=987508 + 479 = 987。

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

首页