結果

問題 No.1854 Limited Bubble Sort
ユーザー yudedakoyudedako
提出日時 2022-03-14 00:17:47
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 1,303 ms / 2,000 ms
コード長 1,965 bytes
コンパイル時間 15,454 ms
コンパイル使用メモリ 255,296 KB
実行使用メモリ 68,520 KB
最終ジャッジ日時 2023-10-19 11:55:52
合計ジャッジ時間 48,273 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,011 ms
64,140 KB
testcase_01 AC 1,030 ms
64,336 KB
testcase_02 AC 1,057 ms
64,316 KB
testcase_03 AC 1,030 ms
64,284 KB
testcase_04 AC 1,054 ms
64,408 KB
testcase_05 AC 1,013 ms
64,336 KB
testcase_06 AC 1,012 ms
64,340 KB
testcase_07 AC 1,031 ms
64,356 KB
testcase_08 AC 1,044 ms
64,372 KB
testcase_09 AC 1,043 ms
64,376 KB
testcase_10 AC 1,033 ms
64,360 KB
testcase_11 AC 1,218 ms
65,152 KB
testcase_12 AC 1,141 ms
64,668 KB
testcase_13 AC 1,230 ms
65,576 KB
testcase_14 AC 1,143 ms
64,392 KB
testcase_15 AC 1,116 ms
64,456 KB
testcase_16 AC 1,244 ms
66,032 KB
testcase_17 AC 1,236 ms
66,108 KB
testcase_18 AC 1,255 ms
66,144 KB
testcase_19 AC 1,247 ms
66,036 KB
testcase_20 AC 1,250 ms
66,192 KB
testcase_21 AC 1,178 ms
65,048 KB
testcase_22 AC 1,303 ms
67,512 KB
testcase_23 AC 1,293 ms
66,156 KB
testcase_24 AC 1,266 ms
68,520 KB
testcase_25 AC 1,105 ms
66,524 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


def solve(permutation: Array[Int]): Option[ArrayBuffer[Int]] =
  if permutation.forall(permutation.indices.contains(_)) then
    permutation.indices.find(i => permutation(i) == i) match
      case Some(i) =>
        solve(permutation.slice(0, i)).flatMap(result => solve(permutation.slice(i + 1, permutation.length).map(_ - i - 1)).map(_.map(_ + i + 1) ++ result))
      case None  =>
        Some(solveFlat(permutation))
  else
    None

def solveFlat(array: Array[Int]): ArrayBuffer[Int] =
  if array.isEmpty then
    return ArrayBuffer.empty
  val indices = Array.fill(array.length){0}
  val leftCount = Array.fill(array.length){0}
  for (a, i) <- array.zipWithIndex do
    indices(a) = i
    leftCount(a) = (0 until i).count(j => array(j) < array(i))
  val result = ArrayBuffer[Int]()
  val current = array.clone()
  def swap(i: Int) =
    if current(i) < current(i + 1) then
      leftCount(current(i + 1)) -= 1
    else
      leftCount(current(i)) += 1
    val temp = current(i)
    current(i) = current(i + 1)
    current(i + 1) = temp
    indices(current(i)) = i
    indices(current(i + 1)) = i + 1
    result.addOne(i + 1)
  for i <- array.indices do
    for j <- i until indices(i) do
      if current(j) != j + 1 && current(j + 1) == j + 2 && leftCount(j + 2) + 1 != j + 2 then
        swap(j)
    for j <- indices(i) until i by -1 do
      swap(j - 1)
  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(permutation) match
      case Some(result) =>
        println(result.size)
        println(result.mkString(" "))
      case None =>
        println(-1)
0