結果

問題 No.1311 Reverse Permutation Index
ユーザー ciel
提出日時 2020-06-07 01:29:19
言語 Ruby
(3.4.1)
結果
AC  
実行時間 95 ms / 1,500 ms
コード長 568 bytes
コンパイル時間 346 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 12,288 KB
最終ジャッジ日時 2024-12-23 14:37:06
合計ジャッジ時間 1,783 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 6
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:38: warning: `*' interpreted as argument prefix
Syntax OK

ソースコード

diff #

#!/usr/bin/ruby
def perm2idx(a)
	lst=[*1..a.size]
	raise if lst!=a.sort
	fact=[1]
	(1...a.size).each{|i|fact<<fact[-1]*i}
	fact.reverse!
	r=0
	a.each_with_index{|e,i|
		idx=lst.index(e)
		r+=fact[i]*idx
		lst.delete_at(idx)
	}
	r
end
def idx2perm(n,siz)
	lst=[*1..siz]
	fact=[1]
	(1...siz).each{|i|fact<<fact[-1]*i}
	fact.reverse!
	raise if fact[0]*siz<=n
	a=[]
	fact.each{|e|
		z=n/e
		n%=e
		a<<lst[z]
		lst.delete_at(z)
	}
	a
end
def reverseperm(a)
	r=[nil]*a.size
	a.size.times{|i|
		r[a[i]-1]=i+1
	}
	r
end
p perm2idx reverseperm idx2perm *gets.split.map(&:to_i)
0