Google Code Jam Notes - Minimum Scalar Product - Java

本文探讨了如何通过重新排列两个向量的元素来获得最小化的标量乘积。介绍了问题背景、输入输出格式及样例,并提供了一种有效的算法实现,通过排序向量元素并按逆序配对的方法来求得最小标量乘积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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: 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


Analysis:
This is actually a mathematical problem, the fact is if we have two arrays in natural order:
(x1, x2, x3, x4) and (y1, y2, y3, y4)
The minimum scalar product is: x1* y4 + x2*y3 + x3*y2 + x4*y1
The maximum scalar product is: x1*y1 + x2*y2 + x3* y3 + x4*y4   

My solution: (Your opinion is highly appreciated)

package codeJam.google.com;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author Zhenyi 2013 Dec 21, 2013 14:45:21 PM
 */
public class MinimumScalarProduct {
	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new FileReader(
				"C:/Users/Zhenyi/Downloads/A-small-practice.in"));
		FileWriter out = new FileWriter(
				"C:/Users/Zhenyi/Downloads/A-small-practice.out");
		// BufferedReader in = new BufferedReader(new
		// FileReader("C:/Users/Zhenyi/Downloads/A-large-practice.in"));
		// FileWriter out = new
		// FileWriter("C:/Users/Zhenyi/Downloads/A-large-practice.out");

		int N = new Integer(in.readLine());

		for (int cases = 1; cases <= N; cases++) {
			int num = new Integer(in.readLine());
			String[] sta = new String(in.readLine()).split("\\s");
			String[] stb = new String(in.readLine()).split("\\s");
			long a[] = new long[num];
			for (int i = 0; i < num; i++) {
				a[i] = new Long(sta[i]);
			}

			long b[] = new long[num];
			for (int i = 0; i < num; i++) {
				b[i] = new Long(stb[i]);
			}

			long result = 0;

			Arrays.sort(a);
			Arrays.sort(b);

			for (int i = 0; i < a.length; i++) {
				result = result + a[i] * b[b.length - i - 1];
			}

			out.write("Case #" + cases + ": " + result + "\n");

		}
		in.close();
		out.flush();
		out.close();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值