結果

問題 No.693 square1001 and Permutation 2
コンテスト
ユーザー mizocoder
提出日時 2018-07-01 20:55:51
言語 Ruby
(4.0.1)
コンパイル:
ruby -w -c _filename_
実行:
ruby _filename_
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 418 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 315 ms
コンパイル使用メモリ 9,216 KB
実行使用メモリ 14,848 KB
最終ジャッジ日時 2026-03-21 16:50:00
合計ジャッジ時間 1,273 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 9
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #
raw source code

n = gets.to_i
a = gets.chomp.split.map(&:to_i)
ans = 0
ary = []

n.times do |i|
	ary[i] = a.count(i+1)
end

n.times do |i|
	if ary[i] > 1
		surplus = ary[i] - 1
		prei = i

		while surplus > 0
			i += 1 while ary[i] != 0
			ary[i] = 1
			ans += i - prei
			surplus -= 1
		end
		
		next
	end

	if ary[i] == 0
		ary[i] = 1
		prei = i
		i += 1 while ary[i] < 2
		ary[i] -= 1
		ans += i - prei

		next
	end
end

print ans
0