-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlast_hit.py
34 lines (29 loc) · 1014 Bytes
/
last_hit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Copyright (c) 2021 kamyu. All rights reserved.
#
# Google Code Jam 2014 Round 3 - Problem B. Last Hit
# https://codingcompetitions.witH_Google.com/codejam/round/000000000043371f/0000000000433a3e
#
# Time: O(N^2 * MAX_H/Q)
# Space: O(N * MAX_H/Q)
#
from collections import defaultdict
def ceil_divide(a, b):
return (a+(b-1))//b
def last_hit():
P, Q, N = map(int, raw_input().strip().split())
H_G = [map(int, raw_input().strip().split()) for _ in xrange(N)]
dp = defaultdict(int) # dp[i]: max amount of gold with i free shots
dp[1] = 0
for H, G in H_G:
cnt = ceil_divide(H, Q)-1
new_dp = defaultdict(int)
for i, v in dp.iteritems():
new_dp[i+cnt+1] = v
free = cnt-ceil_divide(H-Q*cnt, P)
for i, v in dp.iteritems():
if i+free >= 0:
new_dp[i+free] = max(new_dp[i+free], v+G)
dp = new_dp
return max(dp.itervalues())
for case in xrange(input()):
print 'Case #%d: %s' % (case+1, last_hit())