// The array size is limited to 200k elements, but that could be // 200k * (200k - 1) / 2 inversions, so we need to use long when counting. public static long Solve(int[] array) { var inversionBST = new InversionBST(array[0]); long inversionCount = 0; for (int i = 1; i < array.Length; ++i) { inversionCount += inversionBST.Add(array[i]); } return inversionCount; }
// The array size is limited to 200k elements, but that could be // 200k * (200k - 1) / 2 inversions, so we need to use long when counting. public static long Solve(int[] array) { var inversionBST = new InversionBST(array[0]); long inversionCount = 0; for (int i = 1; i < array.Length; ++i) { inversionCount += inversionBST.Add(array[i]); } return(inversionCount); }
// We can map Yoda's words to numbers using the ordered statement. Once // we do that we need to count the number of inversions in the resulting // array, just like CODESPTB or INVCNT. public static int Solve(int wordCount, string[] yodaStatement, string[] orderedStatement) { var wordsOrderedIndices = new Dictionary <string, int>(wordCount); for (int w = 0; w < wordCount; ++w) { wordsOrderedIndices[orderedStatement[w]] = w; } int[] yodaStatementWordIndices = yodaStatement .Select(w => wordsOrderedIndices[w]) .ToArray(); var inversionBST = new InversionBST(yodaStatementWordIndices[0]); int inversionCount = 0; for (int i = 1; i < wordCount; ++i) { inversionCount += inversionBST.Add(yodaStatementWordIndices[i]); } return(inversionCount); }