Friday, April 12, 2013

Google Code Jam Round 1A 2008 A Minimum Scalar Product

// Google Code Jam Round 1A 2008 A Minimum Scalar Product
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

//Google Code Jam
public class GoogleCode1 {
    private static MyScanner in;
    private static PrintStream out;

    private static void solve() throws IOException
    {
        int C = in.nextInt();
        in.nextLine();
        for (int i = 0; i < C; i++) {
            out.print("Case #" + (i + 1) + ": ");
            solveCase();
        }
    }

    private static void solveCase() throws IOException {
        int n = in.nextInt();
        long[] x = new long[n];
        long[] y = new long[n];
        for (int i = 0; i < n; i++) {
            x[i] = in.nextLong();
        }
        for (int i = 0; i < n; i++) {
            y[i] = in.nextLong();
        }
        Arrays.sort(x);
        Arrays.sort(y);
        long sum = 0;
        for (int i = 0; i < n; i++) {
            sum += x[i] * y[n - 1 - i];
        }
        out.println(sum);
    }

    public static void main(String[] args) throws IOException {
        // helpers for input/output
        boolean usingFileForIO = true;
        if (usingFileForIO) {
            // using input.txt and output.txt as I/O
            in = new MyScanner("E:\\zin.txt");
            out = new PrintStream("E:\\zout.txt");
        }
        else {
            in = new MyScanner();
            out = System.out;
        }

        solve();
    }

    // =====================================
    static class MyScanner {
        Scanner inp = null;

        public MyScanner() throws IOException
        {
            inp = new Scanner(System.in);
        }

        public MyScanner(String inputFile) throws IOException {
            inp = new Scanner(new FileInputStream(inputFile));
        }

        public int nextInt() throws IOException {
            return inp.nextInt();
        }

        public long nextLong() throws IOException {
            return inp.nextLong();
        }

        public double nextDouble() throws IOException {
            return inp.nextDouble();
        }

        public String nextString() throws IOException {
            return inp.next();
        }

        public String nextLine() throws IOException {
            return inp.nextLine();
        }
    }

}

No comments:

Post a Comment