Submission #4026841


Source Code Expand

import java.io.{InputStream, OutputStream, PrintStream}
import java.util.Scanner

import scala.annotation.tailrec

object Main {

  def main(args: Array[String]): Unit = {
    val solver = new Solver(System.in, System.out)
    solver.solve()
  }

  class Solver(in: InputStream, out: OutputStream) {
    lazy val input = new Scanner(in)

    lazy val output = new PrintStream(out)

    def solve(): Unit = {
      val Array(n, k) = input.nextLine().split(" ").map(_.toInt)
      val xs = input.nextLine().split(" ").map(_.toInt)

      output.println(
        solve(
          xs.filter(_ < 0).map(_ * -1).reverse,
          xs.filter(_ >= 0),
          k)
      )
    }

    def solve(lefts: Array[Int], rights: Array[Int], k: Int): Int = {
      var leftPos, rightPos = 0
      var leftIdx, rightIdx = 0
      var foundCount = 0

      if (rights.headOption.getOrElse(-1) == 0) {
        foundCount += 1
      }

      if (foundCount >= k) {
        return 0
      }

      1.to(Math.pow(10, 8).toInt).foreach { i =>
        if (rights.applyOrElse(rightIdx, (_: Int) => -1) == i) {
          rightPos = i
          rightIdx += 1
          foundCount += 1
        }

        if (foundCount >= k) {
          val min = leftPos.min(rightPos)
          val max = leftPos.max(rightPos)
          return min * 2 + max
        }

        if (lefts.applyOrElse(leftIdx, (_: Int) => -1) == i) {
          leftPos = i
          leftIdx += 1
          foundCount += 1
        }

        if (foundCount >= k) {
          val min = leftPos.min(rightPos)
          val max = leftPos.max(rightPos)
          return min * 2 + max
        }
      }

      0
    }

    @tailrec
    final def gcd(m: Int, n: Int): Int = {
      val (x, y) = if (m > n) (m, n) else (n, m)
      if (y != 0) gcd(y, x % y)
      else x
    }

  }

}

Submission Info

Submission Time
Task C - Candles
User lambdasawa
Language Scala (2.11.7)
Score 0
Code Size 1895 Byte
Status WA
Exec Time 1975 ms
Memory 122496 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 300
Status
AC × 4
AC × 9
WA × 3
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt
Case Name Status Exec Time Memory
0_00.txt AC 345 ms 25380 KB
0_01.txt AC 342 ms 25404 KB
0_02.txt AC 339 ms 25272 KB
0_03.txt AC 344 ms 25380 KB
1_00.txt AC 1600 ms 118588 KB
1_01.txt WA 1218 ms 116164 KB
1_02.txt AC 1975 ms 122204 KB
1_03.txt AC 1390 ms 122244 KB
1_04.txt AC 1952 ms 120220 KB
1_05.txt AC 1361 ms 117956 KB
1_06.txt WA 1409 ms 122496 KB
1_07.txt WA 1426 ms 122116 KB