Friday 27 March 2015

Google code jam :Minimum Scalar Product

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 ≤ xi, yi ≤ 1000
Large dataset
T = 10
100 ≤ n ≤ 800
-100000 ≤ xi, yi ≤ 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



Trick
1.sort array one in ascending order
2.sort array one in descending order
3.multiply  and add them in special order;
package minimumScalarProduct;

public class InsertionSort {
 public long arr[];

 InsertionSort(int i) {
  arr = new long[i];
 }

 long[] insertionSort(boolean flag) {
  long key;
  for (int i = 1, j = 0; i < arr.length; i++) {
   key = arr[i];
   j = i;
   while (j > 0 && compare(flag, key, arr[j - 1])) {// swap
    arr[j] = arr[j - 1];
    j--;
   }
   arr[j] = key;
  }
  return arr;
 }

 boolean compare(boolean flag, long key, long arr) {
  if (!flag)
   return key > arr;
  else
   return key < arr;

 }

}

package minimumScalarProduct;

import java.util.Arrays;
import java.util.StringTokenizer;

public class MinimumScalarProduct {
 long vec1[];
 long vec2[];
 int size;
 long mulsum;

 public MinimumScalarProduct(String vector1, String vector2, int size) {
  super();
  StringTokenizer st1 = new StringTokenizer(vector1, " ");
  StringTokenizer st2 = new StringTokenizer(vector2, " ");
  int i = 0;
  vec1 = new long[size];
  vec2 = new long[size];
  while (st1.hasMoreTokens()) {
   this.vec1[i] = Integer.parseInt(st1.nextToken());
   this.vec2[i++] = Integer.parseInt(st2.nextToken());
  }

  this.size = size;
  System.out.println("printing");
  InsertionSort is = new InsertionSort(6);
  is.arr = vec1;
  vec1 = is.insertionSort(true);// true asc. false des.
  System.out.println(Arrays.toString(vec1));
  is.arr = vec2;
  vec2 = is.insertionSort(false);
  System.out.println(Arrays.toString(vec2));
 }

 long mul() {
  mulsum = 0;
  int i = 0, j = size / 2;
  for (; i < size / 2 && j < size; i++, j++) {
   mulsum += vec1[i] * vec2[i];
   mulsum += vec1[j] * vec2[j];
  }
  if (size % 2 == 1)
   mulsum += vec1[size - 1] * vec2[size - 1];
  System.out.println("sum:" + mulsum);
  return mulsum;

 }

}

package minimumScalarProduct;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;

public class MinimumProductManager {
 private static Scanner s;
 static File f1;// This class is used for creation of files and directories,
 static File f2; // file searching, file deletion etc.
 static int cases;// total no. of cases
 private static PrintWriter pw;

 public static void main(String[] args) {
  f1 = new File("A-large-practice.in");
  f2 = new File("A-large-practice.out");
  // below code might throw anexception
  try {
   s = new Scanner(f1);
   pw = new PrintWriter(f2);
   cases = Integer.parseInt(s.nextLine());
   int num = 1;// like case #num
   int vecSize;
   while (cases != 0) {
    vecSize = Integer.parseInt(s.nextLine());
    MinimumScalarProduct msp = new MinimumScalarProduct(
      s.nextLine(), s.nextLine(), vecSize);
    pw.println("Case #" + num++ + ": " + msp.mul());
    cases--;
   }
   s.close();
   pw.close();
  } catch (FileNotFoundException e) {
   System.out.println("FileNotFoundException ");
  }
 }

}