Created
November 28, 2014 13:30
-
-
Save sungkmi/df458a80d154a7d9bdbf 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 ThemePark extends App { | |
@annotation.tailrec | |
def pickRide(k: Int, gs: List[Int], picked: List[Int] = Nil): (List[Int], List[Int]) = { | |
if (gs.isEmpty || gs.head > k) (picked.reverse, gs) | |
else { | |
pickRide(k-gs.head, gs.tail, gs.head :: picked) | |
} | |
} | |
@annotation.tailrec | |
def euro(r: Int, k: Int, gs: List[Int], acc:Int=0): Int = { | |
if( r == 0) acc | |
else { | |
var (a,b) = pickRide(k, gs) | |
euro(r - 1, k, b ::: a, acc + a.sum) | |
} | |
} | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = | |
for (i <- 1 to lineIn.next().toInt) { | |
val Array(r, k, n) = lineIn.next().split(' ').map(_.toInt) | |
val gs = lineIn.next().split(' ').map(_.toInt).toList | |
lineOut(s"Case #$i: ${euro(r, k, gs)}") | |
} | |
val writer = new java.io.PrintWriter("c.large.out") | |
try { | |
process(io.Source.fromFile("C-large-practice.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 ThemePark._ | |
class ThemeParkTest extends FunSuite { | |
test("pick ride") { | |
assert(pickRide(6, List(1, 4, 2, 1)) === (List(1, 4), List(2, 1))) | |
} | |
test("sample #1") { | |
assert(euro(4, 6, List(1, 4, 2, 1)) === 21) | |
} | |
test("sample #2") { | |
assert(euro(100, 10, List(1)) === 100) | |
} | |
test("sample #3") { | |
assert(euro(5, 5, List(2, 4, 2, 3, 4, 2, 1, 2, 1, 3)) === 20) | |
} | |
test("large #4") { | |
assert(euro(100000000, 2, List(1, 2)) === 100000000 / 2 * 3) | |
} | |
test("sample case") { | |
val input = """3 | |
4 6 4 | |
1 4 2 1 | |
100 10 1 | |
1 | |
5 5 10 | |
2 4 2 3 4 2 1 2 1 3""".lines | |
val expected = """Case #1: 21 | |
Case #2: 100 | |
Case #3: 20""".lines | |
lineComparison(input, expected) | |
} | |
test("full small case") { | |
val input = io.Source.fromFile("C-small-practice.in").getLines() | |
val expected = io.Source.fromFile("c.small.out.ref").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.out.ref").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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment