Code Jam 2008 Round1A Problem A. Minimum Scalar Product —— 贪心

来源:互联网 发布:淘宝哪家二手3ds 编辑:程序博客网 时间:2024/04/30 09:37

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 nintegers 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 ≤ xiyi ≤ 1000

Large dataset

T = 10
100 ≤ n ≤ 800
-100000 ≤ xiyi ≤ 100000

Sample


Input 
 
Output 
 2
3
1 3 -5
-2 4 1
5
1 2 3 4 5
1 0 1 0 1

Case #1: -25
Case #2: 6

简单的 贪心,给你两个数组,让你调整其中的位置使得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 1000000007typedef 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;}



原创粉丝点击