Minimum Scalar Product (白书)

You are given two vectors v1=(x1,x2,...,xn) and v2=(y1,y2,...,yn). The scalar product of these vectors is a single number, calculated as x1y1+x2y2+...+xnyn.

Suppose you are allowed to permute the coordinates of each vector as you wish. Choose two permutations such that the scalar product of your two new vectors is the smallest possible, and output that minimum scalar product.

Input

There are multiple test cases.

For each test case, the first line contains integer number n. The next two lines contain n integers each (1<=n<=800), giving the coordinates of v1 and v2 respectively.

 Process to the end of file.

Output

For each test case, output a line X, where X is the minimum scalar product of all permutations of the two given vectors.

Sample Input
3
1 3 -5
-2 4 1
5
1 2 3 4 5
1 0 1 0 1

Sample Output

-25
6

求最小的内积:

首先我们将v1升序排,v2按降序排,最后跟据内积的规则乘下即可。

有个坑点就是:数据必须为long long

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

typedef long long LL;

const int maxn=805;

LL v1[maxn];
LL v2[maxn];

int cmp(const int a,const int b)
{
	return a>b;
}

int main()
{
	int n;
	while(~scanf("%d",&n)){
		for(int i=0;i<n;i++){
			scanf("%lld",&v1[i]);
		}
		for(int i=0;i<n;i++){
			scanf("%lld",&v2[i]);
		}
		sort(v1,v1+n,cmp);
		sort(v2,v2+n);
		LL res=0;
		for(int i=0;i<n;i++){
			res+=v1[i]*v2[i];
		}
		printf("%lld\n",res);
	}
	return 0;
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页