-
-
Save theodoreLee/8029b8539666b45e6d3b 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 | |
import scala.io.Source | |
object Fairland { | |
val INPUT = "test.in" | |
val OUTPUT = INPUT.takeWhile(_ != '.') + ".out" | |
val isConsole = true | |
def main(args: Array[String]): Unit = { | |
val itr = Source.fromFile(INPUT).getLines() | |
val stream = if (isConsole) Console.out else new FileOutputStream(OUTPUT) | |
try { | |
Console.withOut(stream) { | |
val sets = itr.next().toInt | |
(1 to sets).foreach { set => | |
val Array(n,d) = itr.next().split(' ').map(_.toInt) | |
val Array(sInit, as,cs,rs) = itr.next().split(' ').map(_.toInt) | |
val Array(mInit, am,cm,rm) = itr.next().split(' ').map(_.toInt) | |
var sBefore = sInit | |
var mBefore = mInit | |
val list = for { | |
i <- (1 until n).toList | |
} yield { | |
mBefore = (mBefore * am + cm) % rm | |
sBefore = (sBefore * as + cs) % rs | |
(i, mBefore % i, sBefore) | |
//m, s | |
} | |
val allList = (0, -1, sInit) :: list | |
println(f"Case #$set: ${numberOfEmployeee(allList, d)}") | |
} | |
} | |
} finally { | |
stream.flush() | |
if (!isConsole) stream.close() | |
} | |
} | |
def numberOfEmployeee(list:List[(Int,Int,Int)], d:Int) = { | |
val childList = list.groupBy(_._2).toMap | |
((list.head._3, list.head._3 + d) :: search(childList, 0, list.head._3, list.head._3 + d)).foreach(x=> println(x._1,x._2)) | |
def search(map:Map[Int,List[(Int,Int,Int)]], current:Int, mMin:Int, mMax:Int):List[(Int,Int)] = map.get(current) match { | |
case None => Nil | |
case Some(list) => | |
val ret = list.map(x => | |
if(x._3 + d < mMin || x._3 > mMax) Nil | |
else { | |
val newMin = x._3 max mMin | |
val newMax = mMax min x._3 + d | |
println(newMin,newMax, x._3) | |
(newMin,newMax) :: search(map, x._1, newMin,newMax ) | |
} | |
) | |
ret.flatten | |
} | |
1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment