Sort by

recency

|

10 Discussions

|

  • + 0 comments

    scala and the power of PriorityQueue :)

    import scala.collection.mutable.PriorityQueue
    
    object Solution {
      def main(args: Array[String]): Unit = {
        val in = Iterator
          .continually(io.StdIn.readLine())
          .takeWhile(null != _)
          .map(_.trim)
        val Array(n, q) = in.next().split(' ').map(_.toInt)
        val armies = Array.fill(n)(PriorityQueue.empty[Int])
        def findStrongest(i: Int): Unit = println(armies(i - 1).headOption.orNull)
        def strongestDied(i: Int): Unit = armies(i - 1).dequeue()
        def recruit(i: Int, c: Int): Unit = armies(i - 1).addOne(c)
        def merge(i: Int, j: Int): Unit = armies(i - 1).addAll(armies(j - 1).dequeueAll)
        for (_ <- 1 to q) {
          in.next().split(' ').map(_.toInt) match {
            case Array(1, i)    => findStrongest(i)
            case Array(2, i)    => strongestDied(i)
            case Array(3, i, c) => recruit(i, c)
            case Array(4, i, j) => merge(i, j)
            case event          => throw new RuntimeException(s"invalid event type ${event.headOption.orNull}")
          }
        }
      }
    }
    
  • + 0 comments

    Test 11 is giving "Wrong answer", but when I run it locally I get no differences from the expected output.

    I am also getting "Wrong answer" for all later tests, but haven't yet investigated.

    Any ideas?

  • + 0 comments

    Hey Guys, could anyone help me? I submitted Scala solution for this where I used mutable heap and InputStreamReader and BufferedStreamReader, but test-cases: 16, 18, 19 are still time-outed... I don't know how I could deal with IO more efficiently. I guess, it's the IO causing these timeouts.

    Any help would be great.

    Thank you!

  • + 3 comments

    Tests 16, 17, 18, 19 always return "Runtime Error". It seems to be caused by "time limit" since those tests run correctly on my laptop. But I have already optimized code with IOVector and ByteString-based IO. I have tried various logarithmic heap structures including LeftiestHeap, BinomialHeap, and IntMap. No luck! Anyone facing the same problem here?

  • + 1 comment

    what's the fastest of way of reading input using scala ? i've tried couple of options using readLine, Source.stdin.getLines but all of them ended with timeouts for 4 of the tests (16,17,18,19 to be specific)..