-
-
Save krikit/44216f0e4d093a395bef 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
object TextMessagingOutrage extends App { | |
// incorrect results!!! | |
def groupByK(sortedFreq: List[Int], K: Int): List[List[Int]] = { | |
def padZero(freq: List[Int]): List[Int] = { | |
val remainder = freq.size % K | |
freq ++ Array.fill(remainder)(0).toList | |
} | |
if (sortedFreq.size < K) List(sortedFreq) | |
else { | |
val paddedFreq = padZero(sortedFreq) | |
(for (i <- 0 until paddedFreq.size / K) yield { | |
(for (j <- 0 until K) yield { | |
paddedFreq(K * i + j) | |
}).toList | |
}).toList | |
} | |
} | |
def calc(groupedFreq: List[List[Int]]): Int = { | |
(for (i <- 0 until groupedFreq.size) yield { | |
val group = groupedFreq(i) | |
(i + 1) * group.sum | |
}).sum | |
} | |
def solve(P: Int, K: Int, L: Int, freq: List[Int]): Int = { | |
val sortedFreq = freq.sorted.reverse | |
val groupedFreq = groupByK(sortedFreq, K) | |
calc(groupedFreq) | |
} | |
////////// | |
// main // | |
////////// | |
if (args.length > 0) Console.setIn(new java.io.FileInputStream(args(0))) | |
if (args.length > 1) Console.setOut(new java.io.FileOutputStream(args(1))) | |
for (t <- 1 to readInt) { | |
val Array(p, k, l) = readLine.split(" ").map(_.toInt) | |
val freq = readLine.split(" ").map(_.toInt).toList | |
println(s"Case #${t}: ${solve(p, k, l, freq)}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment