Problem
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 v 1 and v 2 respectively.Output
For each test case, output a line
Case #X: Ywhere 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 |
简单的 贪心,给你两个数组,让你调整其中的位置使得a[0]*b[0] + a[1] * b[1] + ... 的值最小并输出最小值。
思路就是一个数组按照升序排,另一个按照降序排,然后他们的积之和最小。
#include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <string> #include <stack> #include <cctype> #include <vector> #include <queue> #include <set> #include <utility> using namespace std; #define Online_Judge #define outstars cout << "***********************" << endl; #define clr(a,b) memset(a,b,sizeof(a)) #define lson l , mid , rt << 1 #define rson mid + 1 , r , rt << 1 | 1 //#define mid ((l + r) >> 1) #define mk make_pair #define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++) #define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++) #define REP(i , x , n) for(int i = (x) ; i > (n) ; i--) #define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--) #define bug(s) cout<<"#s = "<< s << endl; const int MAXN = 1010; const long long LLMAX = 0x7fffffffffffffffLL; const long long LLMIN = 0x8000000000000000LL; const int INF = 0x7fffffff; const int IMIN = 0x80000000; #define eps 1e-8 #define mod 1000000007 typedef long long LL; const double PI = acos(-1.0); typedef double D; typedef pair<int , int> pi; ///#pragma comment(linker, "/STACK:102400000,102400000") int a[MAXN] , b[MAXN]; int main() { //ios::sync_with_stdio(false); #ifdef Online_Judge freopen("A-large-practice .in","r",stdin); freopen("A-large-practice.out","w",stdout); #endif // Online_Judge int t , n; cin >> t; FORR(kase , 1 , t) { scanf("%d" , &n); FOR(i , 0 , n)scanf("%d" , &a[i]); FOR(i , 0 , n)scanf("%d" , &b[i]); sort(a , a + n); sort(b , b + n); LL ans = 0; FOR(i , 0 , n)ans += (LL)a[i] * b[n - i - 1]; printf("Case #%d: %I64d\n" , kase , ans); } return 0; }