Hust oj 1754 Minimum Scalar Product(水题)

来源:互联网 发布:九九乘法表c语言编程 编辑:程序博客网 时间:2024/06/05 11:13
Minimum Scalar ProductTime Limit: 1000 MSMemory Limit: 65536 KTotal Submit: 78(40 users)Total Accepted: 41(38 users)Rating: Special Judge: NoDescription

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
31 3 -5-2 4 151 2 3 4 51 0 1 0 1
Sample Output
-256
给你俩数组,问俩数组乘积之和最小是多少
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;const int Maxn = 1000;typedef long long ll;ll a[Maxn];ll b[Maxn];int n;int main(){    while(~scanf("%d",&n))    {        for(int i=0;i<n;i++)        {            scanf("%lld",&a[i]);        }        for(int i=0;i<n;i++)        {            scanf("%lld",&b[i]);        }        sort(a,a+n);        sort(b,b+n);        ll sum = 0;        for(int i=0;i<n;i++)        {            sum += a[i] * b[n-1-i];        }        printf("%lld\n",sum);    }}


                                             
0 0
原创粉丝点击