結果

問題 No.366 ロボットソート
ユーザー simansiman
提出日時 2020-10-16 06:46:32
言語 Ruby
(3.3.0)
結果
AC  
実行時間 224 ms / 2,000 ms
コード長 649 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 11,328 KB
実行使用メモリ 15,348 KB
最終ジャッジ日時 2023-09-28 01:40:56
合計ジャッジ時間 3,942 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
15,264 KB
testcase_01 AC 70 ms
15,176 KB
testcase_02 AC 71 ms
15,264 KB
testcase_03 AC 73 ms
15,180 KB
testcase_04 AC 72 ms
15,136 KB
testcase_05 AC 72 ms
15,260 KB
testcase_06 AC 70 ms
15,168 KB
testcase_07 AC 72 ms
15,044 KB
testcase_08 AC 75 ms
15,072 KB
testcase_09 AC 74 ms
15,152 KB
testcase_10 AC 69 ms
15,240 KB
testcase_11 AC 71 ms
15,140 KB
testcase_12 AC 73 ms
15,104 KB
testcase_13 AC 70 ms
15,200 KB
testcase_14 AC 74 ms
15,008 KB
testcase_15 AC 72 ms
15,072 KB
testcase_16 AC 71 ms
15,012 KB
testcase_17 AC 73 ms
15,160 KB
testcase_18 AC 87 ms
15,232 KB
testcase_19 AC 84 ms
15,296 KB
testcase_20 AC 73 ms
15,136 KB
testcase_21 AC 143 ms
15,148 KB
testcase_22 AC 224 ms
15,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:50: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

N, K = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)

def inversion_count(arr)
  cnt = 0
  n = arr.size

  n.times do
    0.upto(n - 2) do |i|
      next if arr[i] <= arr[i + 1]

      cnt += 1
      arr[i], arr[i + 1] = arr[i + 1], arr[i]
    end
  end

  cnt
end

arrs = []
checked = Array.new(N, false)

N.times do |i|
  next if checked[i]

  arr = []

  i.step(N - 1, K) do |j|
    checked[j] = true
    arr << A[j]
  end

  arrs << arr
end

res = Array.new(N)
ans = 0

arrs.each_with_index do |arr, s|
  ans += inversion_count(arr)

  s.step(N - 1, K) do |i|
    res[i] = arr.shift
  end
end

if res == A.sort
  puts ans
else
  puts -1
end
0