D. Yet Another Minimization Problem(dp,数学公式推导)

2024-06-13 23:04

本文主要是介绍D. Yet Another Minimization Problem(dp,数学公式推导),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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≤i≤n) and swap aiai and bibi.

Let's define the cost of the array aa as ∑ni=1∑nj=i+1(ai+aj)2∑i=1n∑j=i+1n(ai+aj)2. Similarly, the cost of the array bb is ∑ni=1∑nj=i+1(bi+bj)2∑i=1n∑j=i+1n(bi+bj)2.

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

Input

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

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100) — elements of the first array.

The third line of each test case contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤1001≤bi≤100) — elements of the second array.

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

Output

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

Example

input

Copy

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

output

Copy

0
987
914

Note

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.

思路:

参考:

代码:

int a[maxj],b[maxj];
void solve() {int n;int dp[maxj];//一维滚动数组提高效率cin>>n;int all=0,resa=0,sum=0,ans=0;rep(i,1,n){cin>>a[i];all+=a[i];}rep(i,1,n){cin>>b[i];all+=b[i];if(a[i]>b[i])swap(a[i],b[i]);resa+=a[i];ans+=a[i]*a[i];ans+=b[i]*b[i];b[i]-=a[i];sum+=b[i];}int m=sum/2;memset(dp,0,sizeof(dp));rep(i,1,n) {atp(j,m,b[i]) {dp[j]=max(dp[j],dp[j-b[i]]+b[i]);}}int suma=resa+dp[m],sumb=all-suma;ans=ans*(n-2)+suma*suma+sumb*sumb;cout<<ans<<'\n';
}

这篇关于D. Yet Another Minimization Problem(dp,数学公式推导)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1058679

相关文章

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

hdu4826(三维DP)

这是一个百度之星的资格赛第四题 题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1004&cid=500 题意:从左上角的点到右上角的点,每个点只能走一遍,走的方向有三个:向上,向下,向右,求最大值。 咋一看像搜索题,先暴搜,TLE,然后剪枝,还是TLE.然后我就改方法,用DP来做,这题和普通dp相比,多个个向上

hdu1011(背包树形DP)

没有完全理解这题, m个人,攻打一个map,map的入口是1,在攻打某个结点之前要先攻打其他一个结点 dp[i][j]表示m个人攻打以第i个结点为根节点的子树得到的最优解 状态转移dp[i][ j ] = max(dp[i][j], dp[i][k]+dp[t][j-k]),其中t是i结点的子节点 代码如下: #include<iostream>#include<algorithm

hdu4865(概率DP)

题意:已知前一天和今天的天气概率,某天的天气概率和叶子的潮湿程度的概率,n天叶子的湿度,求n天最有可能的天气情况。 思路:概率DP,dp[i][j]表示第i天天气为j的概率,状态转移如下:dp[i][j] = max(dp[i][j, dp[i-1][k]*table2[k][j]*table1[j][col] )  代码如下: #include <stdio.h>#include

usaco 1.1 Broken Necklace(DP)

直接上代码 接触的第一道dp ps.大概的思路就是 先从左往右用一个数组在每个点记下蓝或黑的个数 再从右到左算一遍 最后取出最大的即可 核心语句在于: 如果 str[i] = 'r'  ,   rl[i]=rl[i-1]+1, bl[i]=0 如果 str[i] = 'b' ,  bl[i]=bl[i-1]+1, rl[i]=0 如果 str[i] = 'w',  bl[i]=b

uva 10014 Simple calculations(数学推导)

直接按照题意来推导最后的结果就行了。 开始的时候只做到了第一个推导,第二次没有继续下去。 代码: #include<stdio.h>int main(){int T, n, i;double a, aa, sum, temp, ans;scanf("%d", &T);while(T--){scanf("%d", &n);scanf("%lf", &first);scanf

uva 10025 The ? 1 ? 2 ? ... ? n = k problem(数学)

题意是    ?  1  ?  2  ?  ...  ?  n = k 式子中给k,? 处可以填 + 也可以填 - ,问最小满足条件的n。 e.g k = 12  - 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12 with n = 7。 先给证明,令 S(n) = 1 + 2 + 3 + 4 + 5 + .... + n 暴搜n,搜出当 S(n) >=

uva 10154 DP 叠乌龟

题意: 给你几只乌龟,每只乌龟有自身的重量和力量。 每只乌龟的力量可以承受自身体重和在其上的几只乌龟的体重和内。 问最多能叠放几只乌龟。 解析: 先将乌龟按力量从小到大排列。 然后dp的时候从前往后叠,状态转移方程: dp[i][j] = dp[i - 1][j];if (dp[i - 1][j - 1] != inf && dp[i - 1][j - 1] <= t[i]

uva 10118 dP

题意: 给4列篮子,每次从某一列开始无放回拿蜡烛放入篮子里,并且篮子最多只能放5支蜡烛,数字代表蜡烛的颜色。 当拿出当前颜色的蜡烛在篮子里存在时,猪脚可以把蜡烛带回家。 问最多拿多少只蜡烛。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cs

uva 10069 DP + 大数加法

代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <cl