結果

問題 No.1604 Swap Sort:ONE
ユーザー 小野寺健小野寺健
提出日時 2021-11-14 15:53:23
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,609 ms / 2,000 ms
コード長 253 bytes
コンパイル時間 618 ms
コンパイル使用メモリ 11,380 KB
実行使用メモリ 15,596 KB
最終ジャッジ日時 2023-08-19 22:47:08
合計ジャッジ時間 17,902 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
15,252 KB
testcase_01 AC 79 ms
15,112 KB
testcase_02 AC 80 ms
15,112 KB
testcase_03 AC 80 ms
15,140 KB
testcase_04 AC 80 ms
15,276 KB
testcase_05 AC 84 ms
15,548 KB
testcase_06 AC 1,609 ms
15,384 KB
testcase_07 AC 861 ms
15,256 KB
testcase_08 AC 852 ms
15,336 KB
testcase_09 AC 872 ms
15,484 KB
testcase_10 AC 862 ms
15,576 KB
testcase_11 AC 859 ms
15,236 KB
testcase_12 AC 897 ms
15,596 KB
testcase_13 AC 866 ms
15,360 KB
testcase_14 AC 857 ms
15,344 KB
testcase_15 AC 853 ms
15,384 KB
testcase_16 AC 880 ms
15,336 KB
testcase_17 AC 357 ms
15,148 KB
testcase_18 AC 361 ms
15,256 KB
testcase_19 AC 788 ms
15,480 KB
testcase_20 AC 327 ms
15,052 KB
testcase_21 AC 769 ms
15,420 KB
testcase_22 AC 584 ms
15,360 KB
testcase_23 AC 161 ms
14,976 KB
testcase_24 AC 107 ms
15,140 KB
testcase_25 AC 782 ms
15,384 KB
testcase_26 AC 448 ms
15,356 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N = gets.to_i
P = gets.split(" ").map{|s| s.to_i}

idx = {}

P.each_with_index {|x, i|
	idx[x] = i
}

cnt = 0
0.upto(N-2) {|i|
	while P[i] != i + 1 do
		j = idx[P[i]-1]
		idx[P[i]], idx[P[j]] = j, i 
		P[i], P[j] = P[j], P[i]
		cnt += 1
	end
}

puts cnt
0