Skip to content

Instantly share code, notes, and snippets.

@handrake
Created November 27, 2015 12:41
Show Gist options
  • Save handrake/3a0acd3a98abf723e514 to your computer and use it in GitHub Desktop.
Save handrake/3a0acd3a98abf723e514 to your computer and use it in GitHub Desktop.
import java.io.PrintStream
import scala.io.Source
import scala.collection.mutable.Map
object gRanks {
def solve(p:Int, points:Seq[Int], n:Int, wNames:Seq[(Int, Array[String])], m:Int) = {
val score = Map[String, List[Int]]()
wNames.foreach{ x =>
val atheletes = x._2
val w = x._1
atheletes.zipWithIndex.map( x => (x._1, if (x._2 >= p) 0 else points(x._2) * w)).foreach{ y =>
if (score.contains(y._1)) {
score(y._1) = (y._2 :: score(y._1)).sortBy(-_).take(p)
} else {
score(y._1) = List[Int](y._2)
}
}
}
score.map( z => (z._1, z._2.sum)).toList.sortBy(x=>(-x._2, x._1))
}
def main(args: Array[String]): Unit = {
val INPUT = "A-large-practice.in"
val OUTPUT = INPUT.takeWhile(_ != '.') + ".out"
val isConsole = false
val itr = Source.fromFile(INPUT).getLines()
val stream = if (isConsole) Console.out else new PrintStream(OUTPUT)
try {
val t = itr.next().toInt
(1 to t).foreach { x =>
val p = itr.next().toInt
val points = itr.next().split(" ").map(_.toInt)
val n = itr.next().toInt
stream.println(s"Case #${x}:")
val wNames = for(i <- 1 to n) yield {
val s = itr.next().split(" ")
val w = s(0).toInt
val names = s.tail
(w, names)
}
val m = itr.next().toInt
var count = 1
var lastScore = -1
solve(p, points, n, wNames, m).zipWithIndex.foreach { x =>
if (x._2 > 0 && x._1._2 < lastScore) {
count = x._2 + 1
}
lastScore = x._1._2
stream.println(s"${count}: ${x._1._1}")
}
}
} finally {
stream.flush()
if (!isConsole) stream.close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment