-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2010 S5 - Nutrient Tree.cpp
171 lines (133 loc) · 4.86 KB
/
2010 S5 - Nutrient Tree.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
2010 S5 - Nutrient Tree
Difficulty: Very Hard
Basically for each non-leaf node
It has 2 children
N
/ \
L R
Basically, we make dp array where dp[i][j] tells us the max nutrients, node i can transport with j growth agents given to it
To calculate this value for non leaf nodes, its the dp[i][j] values of its 2 children added together
Note that the edge may be limiting, so take into account the edge as well
If its a leaf node, you just try every amount of growth agent on the node itself and on its edge
for this problem, i think its easier to explain with actual code
*/
#include <iostream>
#include <string>
#include <stack>
#define leftChild node * 2
#define rightChild node * 2 + 1
int X;
int tree [10000];
int dp[10000][2501];
int leftRight[10000][2501];
int answer = 0;
void dfs(int node){
//Leaf node
if (tree[node]){
//For every growth agent amount
for (int i = 0; i <= X; i++){
//From the current amount of growth agent that can be used, try giving every possible value to the edge
for (int e = 0; e <= i; e++){
int nodeGrowth = i - e; //Total growth agents - agents given to edge = growth agents left for node
//Remember that the edge may be limiting, so you have to take min
//dp[node][i] = max(itself, min(node + growth, edge capacity))
dp[node][i] = std::max(dp[node][i], std::min(tree[node] + nodeGrowth, (1 + e) * (1 + e)));
}
}
}
//Not leaf node
else{
//Solve its children first,
dfs(leftChild);
dfs(rightChild);
//For every growth agent amount
for (int i = 0; i <= X; i++){
//For every growth agent amount that can be given to the left child
for (int l = 0; l <= i; l++){
//Growth agent to right child = total amount to be used - amount given to left child
int r = i - l;
//leftRight[node][i] basically tells us the max amount of nutrients that can be produced by the children of the node given i growth agents
//Note that this does not take into account the edge yet, that is in the next loop
leftRight[node][i] = std::max(leftRight[node][i], dp[leftChild][l] + dp[rightChild][r]); //max(itself, its 2 children combined)
//Note that if the current node is the root, then this would also be the answer
if (node == 1){
answer = std::max(answer, leftRight[node][i]);
}
}
}
//For every growth agent amount
for (int i = 0; i <= X; i++){
//From the current amount of growth agent that can be used, try giving every possible value to the edge
for (int e = 0; e <= i; e++){
int nodeGrowth = i - e; //Amount given to edge
//Same idea, max(itself, min(total nutrients from node, edge))
dp[node][i] = std::max(dp[node][i], std::min(leftRight[node][nodeGrowth], (1 + e) * (1 + e)));
}
}
}
}
int main() {
std::string input;
getline(std::cin, input);
std::cin >> X;
//If it's just a root, no subtrees
if (input[0] != '('){
std::cout << std::stoi(input) + X << '\n';
return 0;
}
//Otherwise, we have to deal with this atrocious input format
std::stack<std::string> s; s.push("(");
int node = 1;
int i = 1;
while (!s.empty()){
//Skip blank space
if (input[i] == ' '){
i++;
continue;
}
//Opening bracket
if (input[i] == '('){
//Determine if left or right subtree
if (s.top() == "("){
node = leftChild;
}
else{
node = rightChild;
}
s.push("(");
i++;
}
//End of a subtree
else if (input[i] == ')'){
int right = std::stoi(s.top()); s.pop();
int left = std::stoi(s.top()); s.pop();
s.pop(); //Get rid of the opening bracket
tree[leftChild] = left;
tree[rightChild] = right;
if(!s.empty()) s.push("0"); //Basically used for filler if the node isnt a leaf node
node /= 2; //return to parent
i++;
}
//Get number
else{
std::string num = "";
while (true){
num += input[i];
i++;
if(input[i] == '(' || input[i] == ')' || input[i] == ' '){
s.push(num);
break;
}
}
}
}
/* INPUT DEBUGGING
for (int i = 0 ; i < 20; i++){
std::cout << "Tree[" << i << "] = " << tree[i] << '\n';
}
*/
dfs(1);
std::cout << answer << '\n';
return 0;
}