結果

問題 No.693 square1001 and Permutation 2
ユーザー mizocodermizocoder
提出日時 2018-07-01 20:55:51
言語 Ruby
(3.3.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 418 bytes
コンパイル時間 82 ms
コンパイル使用メモリ 11,500 KB
実行使用メモリ 15,264 KB
最終ジャッジ日時 2023-09-13 16:40:51
合計ジャッジ時間 1,750 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
15,224 KB
testcase_01 AC 85 ms
15,148 KB
testcase_02 AC 83 ms
15,220 KB
testcase_03 AC 83 ms
15,128 KB
testcase_04 AC 85 ms
15,168 KB
testcase_05 AC 85 ms
15,084 KB
testcase_06 AC 83 ms
15,072 KB
testcase_07 AC 83 ms
15,144 KB
testcase_08 AC 83 ms
15,264 KB
testcase_09 AC 83 ms
15,148 KB
testcase_10 AC 84 ms
15,048 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

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