-
-
Save theodoreLee/58704dc57feb9b9956b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileOutputStream | |
import scala.io.Source | |
object UpAndDown { | |
val INPUT = "B-large-practice.in" | |
val OUTPUT = INPUT.takeWhile(_ != '.') + ".out" | |
def solve(list: Array[Int], accr: Int = 0): Int = { | |
if (list.isEmpty) accr | |
else { | |
val idx = list.indexOf(list.min) | |
val cnt = if (list.length / 2 <= idx) list.length - idx - 1 else idx | |
val (head, others) = list.splitAt(idx) | |
solve(head ++ others.tail, accr + cnt) | |
} | |
} | |
def main(args: Array[String]): Unit = { | |
val itr = Source.fromFile(INPUT).getLines() | |
val sets = itr.next().toInt | |
val writer = new FileOutputStream(OUTPUT) | |
// val writer = Console.out | |
try { | |
Console.withOut(writer) { | |
for (set <- 1 to sets) { | |
itr.next() | |
val list = itr.next().split(' ').map(_.toInt) | |
println(f"Case #${set}: ${solve(list)}") | |
} | |
} | |
} finally { | |
writer.close() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment