結果

問題 No.1854 Limited Bubble Sort
コンテスト
ユーザー yudedako
提出日時 2022-03-13 23:13:03
言語 Scala(Beta)
(3.8.1)
コンパイル:
scalac _filename_
実行:
java -cp .:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala3-library_3/3.8.1/scala3-library_3-3.8.1.jar:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala-library/3.8.1/scala-library-3.8.1.jar _class_
結果
WA  
実行時間 -
コード長 1,313 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 12,510 ms
コンパイル使用メモリ 284,620 KB
実行使用メモリ 68,200 KB
最終ジャッジ日時 2026-03-09 20:06:21
合計ジャッジ時間 29,578 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 1 WA * 24
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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