Last active
August 29, 2015 14:23
-
-
Save waynejo/2b618f19d40cfdef7d81 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, FileInputStream} | |
import scala.annotation.tailrec | |
import scala.io.StdIn | |
object Main extends App { | |
Console.setIn(new FileInputStream("C-large-practice (1).in")) | |
Console.setOut(new FileOutputStream("C-large-practice (1).out")) | |
case class ZombieSet(degree: Int, zombie: Int, rpm: Int) | |
case class Zombie(degree: Int, rpm: Int, loop: Int = 0) | |
def toZombie(zombieSet: ZombieSet): List[Zombie] = | |
((0 until zombieSet.zombie) map (x => Zombie(zombieSet.degree, zombieSet.rpm + x))).toList | |
def timeToGoal(zombie: Zombie): Double = | |
zombie.rpm.toDouble / (360.0 / (360.0 - zombie.degree)) + (zombie.loop * zombie.rpm) | |
def meetCount(zombie: Zombie): Int = if (0 == zombie.loop) 1 else zombie.loop - 1 | |
def step(zombies: List[Zombie], minMeetNum: Int): Int = { | |
val meetNum = zombies.map(meetCount).sum | |
if (meetNum >= zombies.length * 2) { | |
minMeetNum | |
} else { | |
val sortedZombies = zombies.sortBy(timeToGoal) | |
val fastestZombie = sortedZombies.head | |
val newZombies = fastestZombie.copy(loop = fastestZombie.loop + 1) :: sortedZombies.tail | |
step(newZombies, minMeetNum min meetNum) | |
} | |
} | |
def solve(zombieSets: List[ZombieSet]): Int = { | |
val zombies = zombieSets.flatMap(toZombie) | |
step(zombies, zombies.length) | |
} | |
val cases = StdIn.readLine().toInt | |
(1 to cases) foreach { n => | |
val teamNum = StdIn.readLine().toInt | |
val zombies = (1 to teamNum) map (_ => { | |
val Array(degree, zombie, rpm) = StdIn.readLine().split(" ").map(_.toInt) | |
ZombieSet(degree, zombie, rpm) | |
}) | |
println(s"Case #$n: ${solve(zombies.toList)}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment