提出 #54698671


ソースコード 拡げる

class PQ
	def initialize
		@h = [] # max-heap
	end

	def enq d,a
		@h << [d,a]
		up_heap
	end
	def deq
		return unless @h[0]
		@h[0],@h[-1] = @h[-1],@h[0]
		d,a = @h.pop
		dn_heap if @h[0]
		return d,a
	end
 
private
	def up_heap
		d, = up = @h[i=@h.size-1]
		while 0<i
			iP = (i-1)/2
			break if d<=@h[iP][0]
			@h[i],i = @h[iP],iP
		end
		@h[i] = up
	end
	def dn_heap
		d, = dn = @h[i=0]
		z = @h.size
		until z <= iC = i+i+1
			iC += 1 if iC+1<z && @h[iC][0]<@h[iC+1][0]
			break if @h[iC][0]<=d
			@h[i],i = @h[iC],iC
		end
		@h[i] = dn
	end
end

(H,W,K),(Si,Sj),*A = $<.map{|ln| ln.split.map(&:to_i) }
IJKL = lambda{|i,j|
	[[i,j-1],[i,j+1],[i-1,j],[i+1,j]].select{|i,j|
		0<=i && i<H && 0<=j && j<W
	}
}
Gi,Gj = Si-1,Sj-1
Q = PQ.new
B = H.times.map{|i|
	W.times.map{|j|
		a = [A[i][j],*IJKL[i,j].map{|i,j| A[i][j] }].max
		b = a*K
		Q.enq b,[i,j,K,a]
		next b
	}
}
while (b,(i,j,k,a) = Q.deq)
	next if b<B[i][j]
	break if i==Gi && j==Gj
	next if k<1
	b,k = b-a+A[i][j],k-1
	IJKL[i,j].each{|i,j|
		Q.enq (B[i][j] = b),[i,j,k,a] if B[i][j]<b
	}
end

p B[Gi][Gj]

提出情報

提出日時
問題 G - AtCoder Tour
ユーザ ds14050
言語 Ruby (ruby 3.2.2)
得点 550
コード長 1132 Byte
結果 AC
実行時間 124 ms
メモリ 18264 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 550 / 550
結果
AC × 2
AC × 36
セット名 テストケース
Sample sample00.txt, sample01.txt
All sample00.txt, sample01.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt
ケース名 結果 実行時間 メモリ
sample00.txt AC 124 ms 17412 KiB
sample01.txt AC 44 ms 17204 KiB
testcase00.txt AC 43 ms 16988 KiB
testcase01.txt AC 51 ms 18136 KiB
testcase02.txt AC 49 ms 17996 KiB
testcase03.txt AC 47 ms 17520 KiB
testcase04.txt AC 51 ms 18012 KiB
testcase05.txt AC 51 ms 18084 KiB
testcase06.txt AC 53 ms 17980 KiB
testcase07.txt AC 55 ms 18188 KiB
testcase08.txt AC 54 ms 18052 KiB
testcase09.txt AC 57 ms 18120 KiB
testcase10.txt AC 51 ms 18100 KiB
testcase11.txt AC 53 ms 18160 KiB
testcase12.txt AC 52 ms 18116 KiB
testcase13.txt AC 55 ms 17932 KiB
testcase14.txt AC 51 ms 18080 KiB
testcase15.txt AC 54 ms 18080 KiB
testcase16.txt AC 52 ms 18204 KiB
testcase17.txt AC 51 ms 17928 KiB
testcase18.txt AC 51 ms 18188 KiB
testcase19.txt AC 50 ms 17564 KiB
testcase20.txt AC 58 ms 18084 KiB
testcase21.txt AC 50 ms 17996 KiB
testcase22.txt AC 55 ms 18128 KiB
testcase23.txt AC 51 ms 17976 KiB
testcase24.txt AC 58 ms 18264 KiB
testcase25.txt AC 49 ms 17696 KiB
testcase26.txt AC 55 ms 18136 KiB
testcase27.txt AC 49 ms 17588 KiB
testcase28.txt AC 55 ms 18072 KiB
testcase29.txt AC 51 ms 17692 KiB
testcase30.txt AC 54 ms 18168 KiB
testcase31.txt AC 53 ms 18084 KiB
testcase32.txt AC 56 ms 18148 KiB
testcase33.txt AC 51 ms 17960 KiB