Created
October 9, 2015 12:29
-
-
Save sungkmi/833704212222ebf88033 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
object GCube extends App { | |
class Cuboid(a: Int*) { | |
def query(l: Int, r: Int): Double = | |
math.exp(a.toSeq.slice(l, r + 1).map(math.log(_)).sum / (r - l + 1)) | |
} | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = | |
for (i <- 1 to lineIn.next().toInt) { | |
def readInts() = lineIn.next().split(' ').map(_.toInt) | |
val Array(n, m) = readInts() | |
val cuboid = new Cuboid(readInts():_*) | |
lineOut(s"Case #$i:") | |
for (j <- 1 to m) { | |
val Array(l, r) = readInts() | |
lineOut(f"${cuboid.query(l, r)}%1.9f") | |
} | |
} | |
val filename = "B-large-practice" | |
val writer = new java.io.PrintWriter(filename + ".out") | |
try { | |
process(io.Source.fromFile(filename + ".in").getLines) { s => | |
writer.println(s); writer.flush() | |
} | |
} finally { | |
writer.flush(); writer.close() | |
} | |
} |
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 org.scalatest._ | |
import GCube._ | |
class GCubeTest extends FunSuite with Matchers { | |
test("sample #1") { | |
val cuboid = new Cuboid(1,4) | |
cuboid.query(0, 0) should equal (1.000000000 +- 1e-6) | |
cuboid.query(0, 1) should equal (2.000000000 +- 1e-6) | |
} | |
test("sample #2") { | |
val cuboid = new Cuboid(1, 2, 3) | |
cuboid.query(0, 1) should equal (1.414213562 +- 1e-6) | |
cuboid.query(1, 2) should equal (2.449489743 +- 1e-6) | |
} | |
test("sample case") { | |
val input = """2 | |
2 2 | |
1 4 | |
0 0 | |
0 1 | |
3 2 | |
1 2 3 | |
0 1 | |
1 2""".lines | |
val expected = """Case #1: | |
1.000000000 | |
2.000000000 | |
Case #2: | |
1.414213562 | |
2.449489743""".lines | |
lineComparison(input, expected) | |
} | |
test("full small case") { | |
val input = io.Source.fromFile("B-small-practice.in").getLines() | |
val expected = io.Source.fromFile("B-small-practice.out").getLines() | |
lineComparison(input, expected) | |
} | |
ignore("full large case") { | |
val input = io.Source.fromFile("A-large-practice.in").getLines() | |
val expected = io.Source.fromFile("A-large-practice.out").getLines() | |
lineComparison(input, expected) | |
} | |
def lineComparison(input: Iterator[String], expected: Iterator[String]) { | |
process(input) { s => | |
for (line <- s.lines) assert(line.trim === expected.next().trim) | |
} | |
assert(expected.hasNext === false, "Finished too fast.") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment