Skip to content

Instantly share code, notes, and snippets.

@waynejo
Created April 10, 2015 12:49
Show Gist options
  • Save waynejo/5d687471a34ab06fb65e to your computer and use it in GitHub Desktop.
Save waynejo/5d687471a34ab06fb65e to your computer and use it in GitHub Desktop.
import java.io.{FileOutputStream, FileInputStream}
import scala.io.StdIn
object Main extends App {
Console.setIn(new FileInputStream("B-large-practice.in"))
Console.setOut(new FileOutputStream("B-large-practice.out"))
def solve(numbers:List[Int]): Int = {
if (1 >= numbers.length) 0
else {
val index = numbers.zipWithIndex.min._2
val right = numbers.length - 1
val moved = if (index < right - index) index else right - index
moved + solve(numbers.take(index) ::: numbers.drop(index + 1))
}
}
val cases = StdIn.readLine().toInt
(1 to cases) foreach { n =>
val qNum = StdIn.readLine().toInt
val numbers = StdIn.readLine().split(" ").map(_.toInt).toList
println(s"Case #$n: ${solve(numbers)}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment