-
-
Save theodoreLee/2f60d2420e4fec330b6b 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 DrearyDesign { | |
def solve(str: String) = { | |
val Array(k, v) = str.split(' ').map(_.toInt) | |
def bSize(r:Int, g:Int):Long = (((r max g) - v max 0) to ((r min g) + v min k)).size.toLong | |
def gbSize(r:Int):Long = ((r - v max 0) to (r + v min k)).map(bSize(r,_)).sum | |
val middleRange = v to (k - v) | |
middleRange.headOption match { | |
case Some(r) => | |
val middleTotal = gbSize(r) * middleRange.size | |
val headTotal = (0 until v).map(gbSize).sum | |
headTotal * 2 + middleTotal | |
case None => (0 to k).map(gbSize).sum | |
} | |
} | |
def main(args: Array[String]): Unit = { | |
val INPUT = "B-large-practice-2.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 => | |
println(f"Case #$set: ${solve(itr.next())}") | |
} | |
} | |
} 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