Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object RopeIntranet {
- def main(args: Array[String]) {
- val writer = new java.io.PrintWriter("a-large.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 ropes = for (j <- 1 to lineIn.next().toInt) yield {
- val Array(lhs, rhs) = lineIn.next().split(" ").map(_.toInt)
- (lhs, rhs)
- }
- lineOut(s"Case #$i: ${countRopes(ropes.toList, 0)}")
- }
- }
- def isCrossed(lhs: (Int, Int), rhs: (Int, Int)): Boolean =
- (lhs._1 - rhs._1) * (lhs._2 - rhs._2) < 0
- def countCrossing(rope: (Int, Int), ropes: List[(Int, Int)]): Int =
- ropes.filter(isCrossed(_, rope)).length
- @annotation.tailrec
- def countRopes(ropes: List[(Int, Int)], count: Int): Int = {
- if (ropes.length < 2) count
- else {
- val count1 = countCrossing(ropes.head, ropes.tail)
- countRopes(ropes.tail, count + count1)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement