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;
}

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

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试