ZeptoLab Code Rush B. Om Nom and Dark Park

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.