Skip to content

Instantly share code, notes, and snippets.

@theodoreLee
Last active August 29, 2015 14:27
Show Gist options
  • Save theodoreLee/78aa3284649992784685 to your computer and use it in GitHub Desktop.
Save theodoreLee/78aa3284649992784685 to your computer and use it in GitHub Desktop.
import java.io.FileOutputStream
import scala.io.Source
object Fairland2 {
case class Employee(id:Int, mi:Int, salary:Int, min:Int, max:Int)
object Employee {
def apply(id:Int, salary:Int, mi:Int, manager:Employee, d:Int): Employee = {
if(manager.min == -1 || salary + d < manager.min || salary > manager.max) Employee(id, mi, salary, -1, -1)
else Employee(id, mi, salary, salary max manager.min, manager.max min salary + d)
}
}
def solve(in: IndexedSeq[String]) = {
val Array(n,d) = in.head.split(' ').map(_.toInt)
val Array(sInit, as, cs, rs) = in(1).split(' ').map(_.toInt)
val Array(mInit, am, cm, rm) = in(2).split(' ').map(_.toInt)
val employees = (Vector.empty[Employee] /: (0 until n)) {
case (vector, i) if i == 0 => Employee(i, mInit, sInit, sInit, sInit + d) +: vector
case (vector, i) =>
val before = vector(i - 1)
val mi = (before.mi * am + cm) % rm
val salary = (before.salary * as + cs) % rs
val manager = vector(mi % i)
vector :+ Employee(i, salary, mi, manager, d)
}
val variations = employees.filterNot(_.min == -1).flatMap { e =>
Vector((e.min, 1), (e.max + 1, -1))
}.groupBy(_._1).map {
case (key, value) => (key, value.map(_._2).sum)
}.toVector.sortBy(_._1)
((0,0) /: variations) {
case ((max, current), (_, variation)) =>
val m = current + variation
(max max m, m)
}._1
}
def main(args: Array[String]): Unit = {
val INPUT = "A-large-practice.in"
val OUTPUT = INPUT.takeWhile(_ != '.') + ".out"
val isConsole = false
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 seq = (1 to 3).map(_ => itr.next())
println(f"Case #$set: ${solve(seq)}")
}
}
} finally {
stream.flush()
if (!isConsole) stream.close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment