当前位置:首页 > 数学 > 正文
CF1637D Yet Another Minimization Problem
679+

题目大意:两个长度为n的数组,数组权值为任意两个数的乘积之和,现在可以交换两个数组相同位置的数,请问两个数组权值之和最小是多少?

题意翻译

定义某数组 $x$ 的权值为

$$\sum\limits_{i=1}^n\sum\limits_{j=i+1}^n(x_i+x_j)^2$$

现在,给定两个长度为 $n$ 的数组 $a,b$。你可以执行若干次操作,每次操作选择一个下标 $i$,并交换 $a_i,b_i$。求在进行操作之后两个数组的权值之和最小能够达到多少。

数据范围:

– $t$ 组数据,$1\leqslant t\leqslant 40$。
– $1\leqslant n,\sum n\leqslant 100$。
– $1\leqslant a_i,b_i\leqslant 100$。

Translated by Eason_AC

题目描述

You are given two arrays $ a $ and $ b $ , both of length $ n $ .

You can perform the following operation any number of times (possibly zero): select an index $ i $ ( $ 1 \leq i \leq n $ ) and swap $ a_i $ and $ b_i $ .

Let’s define the cost of the array $ a $ as $ \sum_{i=1}^{n} \sum_{j=i + 1}^{n} (a_i + a_j)^2 $ . Similarly, the cost of the array $ b $ is $ \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.

输入输出格式

输入格式

Each test case consists of several test cases. The first line contains a single integer $ t $ ( $ 1 \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 $ n $ ( $ 1 \leq n \leq 100 $ ) — the length of both arrays.

The second line of each test case contains $ n $ integers $ a_1, a_2, \ldots, a_n $ ( $ 1 \leq a_i \leq 100 $ ) — elements of the first array.

The third line of each test case contains $ n $ integers $ b_1, b_2, \ldots, b_n $ ( $ 1 \leq b_i \leq 100 $ ) — elements of the second array.

It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 100 $ .

输出格式

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] $ , $ b = [3, 7, 6, 1] $ .

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

The cost of the array $ b $ equals to $ (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 = 987 $ .

解题思路

$(a_i + a_j)^2 = (a_i)^2 + (a_j)^2 + 2 a_i a_j$,全部加起来,会发现每个$a_i$都需要加n-1次,因为他可以和其他n-1个数相乘。剩下的任意两项之和,用乘法分配律,可以发现,$a_i$需要乘上后面数字之和,或者$a_j$需要乘上前面所有数之和。

我们可以暴力枚举每个位置是否交换,并维护两个数组的前缀和,记录最优值即可,加入记忆化就满分了,当然还可以改成倒搜、广搜、DP等。

程序实现

About

坚决不Copy代码!

本文标签:,,,,,,,,

CF1637D Yet Another Minimization Problem:等您坐沙发呢!

发表评论

您必须 [ 登录 ] 才能发表留言!