結果
問題 |
No.1854 Limited Bubble Sort
|
ユーザー |
|
提出日時 | 2022-03-13 23:13:03 |
言語 | Scala(Beta) (3.6.2) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,313 bytes |
コンパイル時間 | 11,386 ms |
コンパイル使用メモリ | 277,396 KB |
実行使用メモリ | 67,636 KB |
最終ジャッジ日時 | 2024-09-19 06:54:17 |
合計ジャッジ時間 | 41,504 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 1 WA * 24 |
ソースコード
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)