結果

問題 No.1854 Limited Bubble Sort
ユーザー yudedakoyudedako
提出日時 2022-03-13 23:13:03
言語 Scala(Beta)
(3.4.0)
結果
WA  
実行時間 -
コード長 1,313 bytes
コンパイル時間 12,332 ms
コンパイル使用メモリ 263,416 KB
実行使用メモリ 67,880 KB
最終ジャッジ日時 2023-10-19 10:43:47
合計ジャッジ時間 46,094 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 974 ms
63,544 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 1,061 ms
65,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import scala.collection.mutable.*
import scala.io.StdIn.*
import scala.util.chaining.*
import scala.math.*
import scala.reflect.ClassTag
import scala.util.*
import scala.annotation.tailrec
import scala.collection.mutable


class Bit(val size: Int):
  private val vec = Array.fill(size){0}
  def get(position: Int): Int =
    var pos = position
    var result = 0
    while pos >= 0 do
      result += vec(pos)
      pos -= ~pos & (pos + 1)
    result
  def add(position: Int, value: Int) =
    var pos = position
    while pos < size do
      vec(pos) += value
      pos += ~pos & (pos + 1)

def solve(n: Int, permutation: Array[Int]): Option[ArrayBuffer[Int]] =
  val result = ArrayBuffer[Int]()
  val bit = Bit(n)
  for (p, i) <- permutation.zipWithIndex do
    if i != p then
      val left = i - bit.get(p)
      result.addAll(i until i - left by - 1)
    else if bit.get(p) != i then
        return None
    bit.add(p, 1)
  Some(result)

@main def main =
  val testCount = readLine().toInt
  for testCase <- 1 to testCount do
    val n = readLine().toInt
    val permutation = readLine().split(' ').map(_.toInt - 1)
    solve(n, permutation) match
      case Some(result) =>
        println(result.size)
        println(result.mkString(" "))
      case None =>
        println(-1)
0