Last active
November 27, 2015 14:08
-
-
Save waynejo/eb7fbdf43bc697e1d092 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
package Main | |
import java.io.{FileOutputStream, FileInputStream} | |
import scala.collection.immutable.{HashMap, IndexedSeq} | |
import scala.io.StdIn | |
object Main extends App { | |
Console.setIn(new FileInputStream("example.in")) | |
Console.setIn(new FileInputStream("A-small-practice.in")) | |
Console.setOut(new FileOutputStream("A-small-practice.out")) | |
Console.setIn(new FileInputStream("A-large-practice.in")) | |
Console.setOut(new FileOutputStream("A-large-practice.out")) | |
case class Result(rank:Int, name:String) | |
def isSameScoreWithLast(manScore: List[(String, Int)], idx: Int): Boolean = { | |
0 != idx && manScore(idx - 1)._2 == manScore(idx)._2 | |
} | |
def solve(s:Array[Int], wAndNames:IndexedSeq[Array[String]], m:Int):List[Result] = { | |
val manAndScores = (HashMap[String, List[Int]]().withDefault(x => Nil) /: wAndNames)((acc, x) => { | |
val (w, names) = (x.head.toInt, x.tail) | |
(acc /: names.zipWithIndex) { | |
case (scores, (name, rank)) => scores + (name -> (s(rank) * w :: scores(name))) | |
} | |
}) | |
val manScore = manAndScores.mapValues(score => | |
score.sorted.reverse.take(m).sum | |
).toList.sortBy{case (man, score) => (-score, man)} | |
val result = manScore.zipWithIndex.map{ | |
case ((name, score), rank) => Result(rank + 1, name) | |
} | |
(List[Result]() /: result.indices){ | |
case (acc, idx) => acc :+ ( | |
if (isSameScoreWithLast(manScore, idx)) { | |
result(idx).copy(rank = acc.last.rank) | |
} else { | |
result(idx) | |
} | |
) | |
} | |
} | |
val cases = StdIn.readLine().toInt | |
(1 to cases) foreach { i => { | |
val p = StdIn.readLine().toInt | |
val s = StdIn.readLine().split(" ").map(_.toInt) | |
val n = StdIn.readLine().toInt | |
val wAndNames = for (i <- 1 to n) yield StdIn.readLine().split(" ") | |
val m = StdIn.readLine().toInt | |
val names: IndexedSeq[Array[String]] = wAndNames | |
println(s"Case #$i:") | |
println(solve(s, wAndNames, m).map(x => s"${x.rank}: ${x.name}").mkString("\n")) | |
}} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment