Created
August 7, 2015 16:03
-
-
Save waynejo/ad9fca5cc8d942db113d 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 fairland | |
object FairlandTest { | |
case class Employee(no: Int, salary: Int, idxOfManager: Int = -1, minX: Int, maxX: Int) | |
def solve(seq: Seq[String]): Int = { | |
val Array(total, limit) = seq.head.split(" ").map(_.toInt) | |
val Array(s0, as, cs, rs) = seq(1).split(" ").map(_.toInt) | |
val Array(m0, am, cm, rm) = seq(2).split(" ").map(_.toInt) | |
def getSalary(prev: Int): Int = (prev * as + cs) % rs | |
def getIdxOfManager(prev: Int): Int = (prev * am + cm) % rm | |
val employees = (Array.fill[Employee](total)(null) /: (0 until total))((result, idx) => { | |
if (0 == idx) { | |
result(0) = Employee(idx, s0, m0, s0, s0 + limit) | |
} else { | |
val salary = getSalary(result(idx - 1).salary) | |
val managerId = getIdxOfManager(result(idx - 1).idxOfManager) | |
val minX = salary max result(managerId % idx).minX | |
val maxX = (salary + limit) min result(managerId % idx).maxX | |
result(idx) = Employee(idx, salary, managerId, minX, maxX) | |
} | |
result | |
}) | |
val addList = (Vector[(Int, Int)]() /: employees)((acc, x) => { | |
if (x.minX > x.maxX) acc else acc :+ (x.minX, 1) :+ (x.maxX, -1) | |
}).sortBy(x => x._1 * 10 + (x._2 * -1) + 1) | |
(Tuple2[Int, Int](0, 0) /: addList)((acc, x) => { | |
(acc._1 + x._2, acc._2 max (acc._1 + x._2)) | |
})._2 | |
} | |
def main(args: Array[String]): Unit = run() | |
def run() { | |
val writer = new java.io.PrintWriter("A-large-practice.out") | |
try { | |
process(io.Source.fromFile("A-large-practice.in").getLines)(writer.println) | |
} finally { | |
writer.flush() | |
writer.close() | |
} | |
} | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = { | |
for (i <- 1 to lineIn.next().toInt) { | |
val input = Seq(lineIn.next(), lineIn.next(), lineIn.next()) | |
lineOut(s"Case #$i: ${solve(input)}") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment