-
Notifications
You must be signed in to change notification settings - Fork 10
/
enclosure.py
56 lines (47 loc) 路 1.52 KB
/
enclosure.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright (c) 2016 kamyu. All rights reserved.
#
# Google Code Jam 2014 Round 1C - Problem C. Enclosure
# https://code.google.com/codejam/contest/3004486/dashboard#s=p2
#
# Time: O(N^2 * M)
# Space: O(1)
#
def empty_triangle(size):
return size * (size + 1) / 2
def min_stones(N, M, K):
if N > M:
(N, M) = (M, N)
# 1. The optimal shape resembles a dianmond:
# --***--
# -*XXX*-
# *XXXXX*
# -*XXXX*
# --*XX*-
# ---**--
#
# 2. The sizes (the length of its side) of
# the empty triangles at the corners may
# be different by at most one size.
# Generate all possible truncated (and perfect) diamond shapes.
res = K
for R in xrange(2, N + 1):
for C in xrange(R, M + 1):
if R * C >= K:
for i in xrange(2 * R):
cover = R * C
# Increase the size of the empty triangle
# at each corner.
cover -= empty_triangle((i + 3) // 4)
cover -= empty_triangle((i + 2) // 4)
cover -= empty_triangle((i + 1) // 4)
cover -= empty_triangle(i // 4)
if cover < K:
break
stones = 2 * (R + C) - 4 - i
res = min(res, stones)
return res
def enclosure():
N, M, K = map(int, raw_input().strip().split())
return min_stones(N, M, K)
for case in xrange(input()):
print 'Case #%d: %s' % (case+1, enclosure())