Problem A. 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
The first line of the input file contains integer number T - the number of test cases. For each test case, the first line contains integer number n. The next two lines contain n integers each, giving the coordinates of v1 and v2 respectively.
Output
For each test case, output a line
Case #X: Y
where X is the test case number, starting from 1, and Y is the minimum scalar product of all permutations of the two given vectors.
Limits
Small dataset
T = 1000
1 ≤ n ≤ 8
-1000 ≤ xi, yi ≤ 1000
Large dataset
T = 10
100 ≤ n ≤ 800
-100000 ≤ xi, yi ≤ 100000
Sample
Input |
Output |
2 |
Case #1: -25 |
![[Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product [Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product](https://image.miaokee.com:8440/aHR0cDovL2IuaGlwaG90b3MuYmFpZHUuY29tL2JhaWtlL3MlM0QxNzcvc2lnbj04NDIzMGZjOTI5MzQzNDliNzAwNjZhODJmZWViMTUyMS84YzEwMDFlOTM5MDEyMTNmMTkwMGEwMGY1NWU3MzZkMTJmMmU5NTM2LmpwZw%3D%3D.jpg?w=700&webp=1)
排序不等式的证明
![[Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product [Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product](https://image.miaokee.com:8440/aHR0cDovL2IuaGlwaG90b3MuYmFpZHUuY29tL2JhaWtlL3MlM0QxNzcvc2lnbj04NDIzMGZjOTI5MzQzNDliNzAwNjZhODJmZWViMTUyMS84YzEwMDFlOTM5MDEyMTNmMTkwMGEwMGY1NWU3MzZkMTJmMmU5NTM2LmpwZw%3D%3D.jpg?w=700&webp=1)
![[Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product [Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product](https://image.miaokee.com:8440/aHR0cDovL2QuaGlwaG90b3MuYmFpZHUuY29tL2JhaWtlL3MlM0QyMzAvc2lnbj02NzA5MGNiYmNkYmY2YzgxZjMzNzJiZWI4YzNmYjFkNy9hYzZlZGRjNDUxZGE4MWNiYzI1MDZiMWU1MzY2ZDAxNjA4MjQzMWFiLmpwZw%3D%3D.jpg?w=700&webp=1)
![[Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product [Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product](https://image.miaokee.com:8440/aHR0cDovL2UuaGlwaG90b3MuYmFpZHUuY29tL2JhaWtlL3MlM0QxMDQvc2lnbj02NjM3NjBlYTZhNjNmNjI0MTg1ZDNkMDNiMzQ1ZWIzMi9kMWEyMGNmNDMxYWRjYmVmZTIyMTk1ZDVhZGFmMmVkZGEyY2M5ZmM4LmpwZw%3D%3D.jpg?w=700&webp=1)
![[Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product [Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product](https://image.miaokee.com:8440/aHR0cDovL2QuaGlwaG90b3MuYmFpZHUuY29tL2JhaWtlL3MlM0QzMzcvc2lnbj03MWMzMDdkMGM5OTVkMTQzZGU3NmUyMjA0NGYxODI5Ni80YmVkMmU3MzhiZDRiMzFjYjQ2NTdmNjg4NmQ2Mjc3ZjlmMmZmODhjLmpwZw%3D%3D.jpg?w=700&webp=1)
#include<stdio.h>
#include<string.h> int i,j,n,m; long long a[],b[]; long long sum; int
pre()
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
sum=;
return ;
} int
init()
{
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%lld",&a[i]);
for(i=;i<=n;i++)
scanf("%lld",&b[i]); return ;
} void
qsort(long long a[],int head,int tail)
{
int i,j;
long long x;
i=head;j=tail;
x=a[head]; while(i<j)
{
while((i<j)&(a[j]>=x)) j--;
a[i]=a[j];
while((i<j)&(a[i]<=x)) i++;
a[j]=a[i];
}
a[i]=x; if(head<(i-)) qsort(a,head,i-);
if((i+)<tail) qsort(a,i+,tail);
} int
main()
{
int casi,cas;
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
scanf("%d",&cas);
for(casi=;casi<=cas;casi++)
{
pre();
init();
qsort(a,,n);
qsort(b,,n); for(i=;i<=n;i++)
sum+=a[i]*b[n-i+]; printf("Case #%d: %lld\n",casi,sum);
}
return ;
}