1. Title Description: Click to open the link
2. How to solve the problem: the game when the problem did not come out, the next morning when the problem was found to be a simple DFS application. The topic requires a minimum number of lights to be added. Suppose we already know the root of the left Dial hand node has a total of suml lamp, the right child node has a total of sumr lamp, then compare D[lson (Root)]+suml and D[rson (root)]+sumr size can be. The number of streetlights that need to be increased is the absolute value of the difference. Return a larger number at the same time to get root total number of lights.
3. Code:
#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std; #define Lson (x) (x) <<1#define Rson (x) ((x) <<1) +1#define N 12int d[1 << n];int n;int cnt;int dfs (int root)//total number of street lights returned to root {int L = 0, r = 0;if (D[lson (Root)) L=dfs (Root)) (Lson), if (root) R=dfs (root), if (Rson (root) &&!d[lson (Root)]) Return 0;if (D[lson (root) + L < D[rson (Root)] + R) {cnt + D[rson (root) + R-d[lson (root)]-L;return D[rson (root) + R;} else{cnt + = D[lson (root)] + L-d[rson (root)]-R;return D[lson (Root)] + L;}} void Solve () {int root = 1;cnt = 0;dfs (root); cout << cnt << Endl;} int main () {//freopen ("T.txt", "R", stdin);while (~SCANF ("%d", &n)) {int len = 1 << (n + 1), for (int i = 2; i < Len; i++) scanf ("%d", &d[i]); solve ();} return 0;}
ZeptoLab Code Rush B. Om Nom and Dark Park