結果

問題 No.366 ロボットソート
ユーザー mai
提出日時 2017-03-03 21:32:09
言語 Ruby
(3.4.1)
結果
WA  
実行時間 -
コード長 373 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2024-06-22 05:17:14
合計ジャッジ時間 3,213 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 WA * 16
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:1: warning: assigned but unused variable - n
Syntax OK

ソースコード

diff #

n,k=gets.split.map(&:to_i)

aa=gets.split.map(&:to_i)

@cnt=0
def solve_sort!(arr,right,hop)
  i=right
  j=right-hop
  while (0<=j)
    if arr[j]>arr[i]
      arr[i],arr[j]=arr[j],arr[i]
      @cnt+=1
    end
    i-=hop
    j-=hop
  end
end

def sorted?(aa)
  aa.inject{|s,e|!s&&s>e ? false : e}
end

k.times{|i|
  solve_sort!(aa,aa.size-1-i,k)
}

p sorted?(aa) ? @cnt : -1
0