Last active
December 1, 2015 06:53
-
-
Save handrake/354cf585e44bc6d5a4ae 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.PrintStream | |
import scala.io.Source | |
object gRanks { | |
def solve(p:Int, points:Seq[Int], n:Int, wNames:Seq[(Int, Array[String])], m:Int) = { | |
wNames.flatMap{case (w, atheletes) => | |
atheletes.zipWithIndex.map{case (name, i) => | |
(name, if (i >= p) 0 else points(i) * w)}} | |
.groupBy(_._1).mapValues(_.unzip._2.sorted.takeRight(m).sum) | |
.toList.sortBy(-_._2).zipWithIndex.groupBy(_._1._2).toList | |
.sortBy(-_._1).flatMap{case (_, e) => | |
e.map{case (x, _) => (e.head._2+1, x._1)}.sortBy(_._2)} | |
} | |
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 | |
solve(p, points, n, wNames, m) .foreach { y => | |
stream.println(s"${y._1}: ${y._2}") | |
} | |
} | |
} 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