-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1100AddingBinaryTree.py
61 lines (51 loc) · 1.83 KB
/
P1100AddingBinaryTree.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
57
58
59
60
61
#https://vijos.org/p/1100
class AddingBinaryTree():
def __init__(self, values, n):
self.values = values
self.n = n
self.preorderResult = []
def addingBinaryTree(self):
n = self.n
scores = [[None for y in range(n)] for x in range(n)]
choices = [[None for y in range(n)] for x in range(n)]
for m in range(n):
scores[m][m] = self.values[m]
choices[m][m] = m
for length in range(2, n + 1):
for start in range(n - length + 1):
if not scores[start][start + length - 1]:
scores[start][start + length - 1] = float('-inf')
for k in range(start, start + length):
if k == start:
curr_score = scores[k + 1][start + length - 1] + self.values[k]
elif k == start + length - 1:
curr_score = scores[start][k - 1] + self.values[k]
else:
curr_score = scores[start][k - 1] * scores[k + 1][start + length - 1] + self.values[k]
if curr_score > scores[start][start + length - 1]:
choices[start][start + length - 1] = k
scores[start][start + length - 1] = curr_score
self.choices = choices
self.scores = scores[0][n - 1]
def preorderPrint(self, n, m):
self.preorderResult.append(self.choices[n][m] + 1)
if self.choices[n][m] - 1 >= n :
self.preorderPrint(n, self.choices[n][m] - 1)
if self.choices[n][m] + 1 <= m :
self.preorderPrint(self.choices[n][m] + 1, m)
def main():
#g=sys.stdin
g = open("P1100", "r")
s=g.read().splitlines()
while '' in s: s.remove('')
while '\n' in s: s.remove('\n')
numNodes = int(s[0])
values = map(int,s[1].split())
tree = AddingBinaryTree(values, numNodes)
tree.addingBinaryTree()
tree.preorderPrint(0, numNodes - 1)
print tree.scores
for item in tree.preorderResult:
print item,
if __name__=='__main__':
main()