Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. object RopeIntranet {
  2. def main(args: Array[String]) {
  3. val writer = new java.io.PrintWriter("a-large.out")
  4. try {
  5. process(io.Source.fromFile("A-large-practice.in").getLines)(writer.println)
  6. } finally {
  7. writer.flush()
  8. writer.close()
  9. }
  10. }
  11.  
  12. def process(lineIn: Iterator[String])(lineOut: String => Unit) = {
  13. for (i <- 1 to lineIn.next().toInt) {
  14. val ropes = for (j <- 1 to lineIn.next().toInt) yield {
  15. val Array(lhs, rhs) = lineIn.next().split(" ").map(_.toInt)
  16. (lhs, rhs)
  17. }
  18. lineOut(s"Case #$i: ${countRopes(ropes.toList, 0)}")
  19. }
  20. }
  21.  
  22. def isCrossed(lhs: (Int, Int), rhs: (Int, Int)): Boolean =
  23. (lhs._1 - rhs._1) * (lhs._2 - rhs._2) < 0
  24.  
  25. def countCrossing(rope: (Int, Int), ropes: List[(Int, Int)]): Int =
  26. ropes.filter(isCrossed(_, rope)).length
  27.  
  28. @annotation.tailrec
  29. def countRopes(ropes: List[(Int, Int)], count: Int): Int = {
  30. if (ropes.length < 2) count
  31. else {
  32. val count1 = countCrossing(ropes.head, ropes.tail)
  33. countRopes(ropes.tail, count + count1)
  34. }
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement