Problem Description
give two groups of numbers, each n.
Adjust the order of the numbers in each group so that the two sets of data are multiplied by the same subscript elements, and then added and minimized. Requires the program to output this minimum value.
For example, two groups of numbers are: 1 3-5 and-2 4 1
Then the minimum value corresponding to the sum of products should be:
( -5) * 4 + 3 * ( -2) + 1 * 1 = -25
Input FormatThe
first line, a number t, represents the number of data groups. After each set of data, first read into an n, the next two rows per row n number, the absolute value of each number is less than or equal to 1000.
n<=8,t<=1000
output Format
A number represents the answer.
Sample Input
231 3-5-2 4 151 2 3 4 0 1 0 1
Sample Output
-256
Code:
#include <iostream>
#include <cstdio>
#include <algorithm>
#define MAXN 10000
using namespace std;
int A1[MAXN];
int A2[MAXN];
int main ()
{
int T, n;
cin>>t;while (t--)
{
int sum = 0;
cin>>n;
for (int i = 0; i < n; i++)
{
cin>>a1[i];
}
for (int j = 0; J < N; j + +)
{
cin>>a2[j];
}
sort (A1, A1 + N);
sort (A2, a2 + N);
for (int i = 0, j = n-1; I < n && J >= 0; i++, j--)
{
sum + = a1[i] * A2[j];
}
cout<<sum<<endl;
}
return 0;
}
Minimum product (basic type)