Submission #58342271


Source Code Expand

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

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

(N,W),*WV = $<.map{|ln| ln.split.map(&:to_i) }

W2V = [0]*(W+1)
WV.group_by(&:shift).each{|dw,vs|
	kx = W/dw
	dvs = PQ.new
	vs.each{|v,|
		(0..kx).map{|k| k*v-k*k }.each_cons(2){|v0,v1|
			dvs.enq v1-v0
		}
	}
	dvs = kx.times.map{ dvs.deq }
	W.downto(0){|w|
		v = W2V[w]
		dv = dvs.dup
		(w+dw).step(W,dw){|w|
			v += dv.shift
			W2V[w] = v if W2V[w]<v
		}
	}
}

p W2V.max

Submission Info

Submission Time
Task F - Knapsack with Diminishing Values
User ds14050
Language Ruby (ruby 3.2.2)
Score 550
Code Size 951 Byte
Status AC
Exec Time 2012 ms
Memory 104860 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 550 / 550
Status
AC × 3
AC × 58
Set Name Test Cases
Sample 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 99 ms 17124 KiB
00_sample_02.txt AC 46 ms 17280 KiB
00_sample_03.txt AC 44 ms 17268 KiB
01_random_01.txt AC 1531 ms 103832 KiB
01_random_02.txt AC 1501 ms 90964 KiB
01_random_03.txt AC 1520 ms 104860 KiB
01_random_04.txt AC 1527 ms 103524 KiB
01_random_05.txt AC 1498 ms 90572 KiB
01_random_06.txt AC 73 ms 19736 KiB
01_random_07.txt AC 710 ms 36016 KiB
01_random_08.txt AC 810 ms 34624 KiB
01_random_09.txt AC 1023 ms 43104 KiB
01_random_10.txt AC 76 ms 20128 KiB
01_random_11.txt AC 707 ms 39044 KiB
01_random_12.txt AC 245 ms 30936 KiB
01_random_13.txt AC 901 ms 39300 KiB
01_random_14.txt AC 1028 ms 45764 KiB
01_random_15.txt AC 651 ms 38948 KiB
01_random_16.txt AC 130 ms 20936 KiB
01_random_17.txt AC 238 ms 28524 KiB
01_random_18.txt AC 581 ms 17736 KiB
01_random_19.txt AC 1006 ms 44104 KiB
01_random_20.txt AC 788 ms 35284 KiB
01_random_21.txt AC 116 ms 18664 KiB
01_random_22.txt AC 101 ms 21360 KiB
01_random_23.txt AC 865 ms 19380 KiB
01_random_24.txt AC 924 ms 21732 KiB
01_random_25.txt AC 114 ms 18948 KiB
01_random_26.txt AC 215 ms 20420 KiB
01_random_27.txt AC 189 ms 21536 KiB
01_random_28.txt AC 844 ms 18980 KiB
01_random_29.txt AC 1021 ms 21228 KiB
01_random_30.txt AC 401 ms 18760 KiB
01_random_31.txt AC 255 ms 18572 KiB
01_random_32.txt AC 118 ms 21588 KiB
01_random_33.txt AC 876 ms 17976 KiB
01_random_34.txt AC 980 ms 21992 KiB
01_random_35.txt AC 86 ms 19420 KiB
01_random_36.txt AC 576 ms 17712 KiB
01_random_37.txt AC 1226 ms 17884 KiB
01_random_38.txt AC 472 ms 17736 KiB
01_random_39.txt AC 2012 ms 17828 KiB
01_random_40.txt AC 514 ms 17692 KiB
01_random_41.txt AC 377 ms 17796 KiB
01_random_42.txt AC 86 ms 17652 KiB
01_random_43.txt AC 1454 ms 17896 KiB
01_random_44.txt AC 1893 ms 17900 KiB
01_random_45.txt AC 345 ms 17596 KiB
01_random_46.txt AC 422 ms 17876 KiB
01_random_47.txt AC 198 ms 17692 KiB
01_random_48.txt AC 158 ms 17608 KiB
01_random_49.txt AC 1870 ms 17984 KiB
01_random_50.txt AC 61 ms 17576 KiB
02_handmade_01.txt AC 1899 ms 17936 KiB
02_handmade_02.txt AC 121 ms 25712 KiB
02_handmade_03.txt AC 100 ms 24908 KiB
02_handmade_04.txt AC 319 ms 19872 KiB
02_handmade_05.txt AC 64 ms 17820 KiB