Sei sulla pagina 1di 2

<!-- saved from url=(0117)http://codeforces-solutions-java.googlecode.com/svn-hi story/r5/trunk/src/main/java/coursera/algo1/week1/Problem1.

java --> <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859 -1"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">pack age coursera.algo1.week1; import java.util.Arrays; import org.apache.commons.lang3.tuple.Pair; import notsandbox.Problem; /** * This file contains all of the 100,000 integers between 1 and 100,000 (inclusi ve) in some order, with no integer * repeated. * * Your task is to compute the number of inversions in the file given, where the ith row of the file indicates the ith * entry of an array. Because of the large size of this array, you should implem ent the fast divide-and-conquer * algorithm covered in the video lectures. The numeric answer for the given inp ut file should be typed in the space * below. So if your answer is 1198233847, then just type 1198233847 in the spac e provided without any space / commas / * any other punctuation marks. * */ public class Problem1 extends Problem { @Override public void run() { int[] input = readInput(); Pair&lt;int[], Long&gt; result = countAndSort(input); out.print(result.getRight()); } public Pair&lt;int[], Long&gt; countAndSort(int[] input) { int n = input.length; if (n == 1) { return Pair.of(input, 0L); } int middle = n / 2; int leftUnsorted[] = Arrays.copyOfRange(input, 0, middle); int rightUnsorted[] = Arrays.copyOfRange(input, middle, n); Pair&lt;int[], Long&gt; left = countAndSort(leftUnsorted); Pair&lt;int[], Long&gt; rigth = countAndSort(rightUnsorted); Pair&lt;int[], Long&gt; split = countSplitAndMerge(left.getLeft(), rigth .getLeft()); return Pair.of(split.getLeft(), left.getRight() + rigth.getRight() + spl it.getRight()); } public Pair&lt;int[], Long&gt; countSplitAndMerge(int[] left, int[] right) { int leftLen = left.length, rightLen = right.length; int sortedOutput[] = new int[leftLen + rightLen];

int leftIndex = 0, rightIndex = 0, resIndex = 0; long inversions = 0; while (leftIndex &lt; leftLen &amp;&amp; rightIndex &lt; rightLen) { if (left[leftIndex] &lt;= right[rightIndex]) { sortedOutput[resIndex] = left[leftIndex]; leftIndex++; resIndex++; // nothing } else { sortedOutput[resIndex] = right[rightIndex]; rightIndex++; resIndex++; int remainedInLeft = leftLen - leftIndex; inversions = inversions + remainedInLeft; } } while (leftIndex &lt; leftLen) { sortedOutput[resIndex] = left[leftIndex]; leftIndex++; resIndex++; } while (rightIndex &lt; rightLen) { sortedOutput[resIndex] = right[rightIndex]; rightIndex++; resIndex++; } return Pair.of(sortedOutput, inversions); } private int[] readInput() { int[] input = new int[100000]; int i = 0; while (scanner.hasNext()) { int nextInt = scanner.nextInt(); input[i] = nextInt; i++; } return input; } } </pre></body></html>

Potrebbero piacerti anche