zoukankan      html  css  js  c++  java
  • ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS

    B. Om Nom and Dark Park

    Time Limit: 1 Sec  Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/526/problem/B

    Description

    Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who likes visiting friends living at the other side of the park. However the dark old parks can scare even somebody as fearless as Om Nom, so he asks you to help him.

    The park consists of 2n + 1 - 1 squares connected by roads so that the scheme of the park is a full binary tree of depth n. More formally, the entrance to the park is located at the square 1. The exits out of the park are located at squares 2n, 2n + 1, ..., 2n + 1 - 1 and these exits lead straight to the Om Nom friends' houses. From each square i (2 ≤ i < 2n + 1) there is a road to the square . Thus, it is possible to go from the park entrance to each of the exits by walking along exactly n roads.

    To light the path roads in the evening, the park keeper installed street lights along each road. The road that leads from square i to square has ai lights.

    Om Nom loves counting lights on the way to his friend. Om Nom is afraid of spiders who live in the park, so he doesn't like to walk along roads that are not enough lit. What he wants is that the way to any of his friends should have in total the same number of lights. That will make him feel safe.

    He asked you to help him install additional lights. Determine what minimum number of lights it is needed to additionally place on the park roads so that a path from the entrance to any exit of the park contains the same number of street lights. You may add an arbitrary number of street lights to each of the roads.


    Input

    The first line contains integer n (1 ≤ n ≤ 10) — the number of roads on the path from the entrance to any exit.

    The next line contains 2n + 1 - 2 numbers a2, a3, ... a2n + 1 - 1 — the initial numbers of street lights on each road of the park. Here ai is the number of street lights on the road between squares i and . All numbers ai are positive integers, not exceeding 100.

    Output

    Print the minimum number of street lights that we should add to the roads of the park to make Om Nom feel safe.

    Sample Input

    2
    1 2 3 4 5 6

    Sample Output

    5

    HINT

    题意

    让你从根走到每个叶子所要的花费都相同,请问最少得增加多少边权?

    题解:

    啊,DFS,自底往上,预处理个前缀和的东西
    然后ans加上左右叶子的差值,然后一直跑DFS就是了

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 200001
    #define mod 10007
    #define eps 1e-9
    //const int inf=0x7fffffff;   //无限大
    const int inf=0x3f3f3f3f;
    /*
    
    int buf[10];
    inline void write(int i) {
      int p = 0;if(i == 0) p++;
      else while(i) {buf[p++] = i % 10;i /= 10;}
      for(int j = p-1; j >=0; j--) putchar('0' + buf[j]);
      printf("
    ");
    }
    */
    //**************************************************************************************
    
    inline ll read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    ll powmod(ll a,ll b) {
        ll ans=1;
        for(ll i=1;i<=b;i++)
            ans*=a;
        return ans;
    }
    ll a[maxn];
    ll dp[maxn];
    ll n;
    void read_dfs(int x)
    {
        if(x>=powmod(2,n))
            return;
        read_dfs(x*2);
        read_dfs(x*2+1);
        a[x*2]+=max(a[x*2*2],a[x*2*2+1]);
        a[x*2+1]+=max(a[(x*2+1)*2],a[(x*2+1)*2+1]);
        //cout<<a[x*2]<<" "<<x*2<<endl;
        //cout<<a[x*2+1]<<" "<<x*2+1<<endl;
    }
    ll dfs(ll x)
    {
        if(dp[x])
            return dp[x];
        if(x>=powmod(2,n))
            return 0;
        ll sum=0;
        dp[x]=dfs(x*2)+dfs(x*2+1)+abs(a[x*2]-a[x*2+1]);
        return dp[x];
    }
    int main()
    {
        cin>>n;
        int k=powmod(2,n);
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<powmod(2,i);j++)
            {
                cin>>a[powmod(2,i)+j];
            }
        }
        read_dfs(1);
        cout<<dfs(1)<<endl;
    }
  • 相关阅读:
    hihocoder #1467 : 2-SAT·hihoCoder音乐节 2-SAT
    hihoCoder#1185 : 连通性·三 tarjan求强联通分量 缩点 dfs/拓扑排序求路径和最大值
    hihoCoder1175 拓扑排序·二 拓扑排序
    012 列表的一些常用操作符
    011,列表2
    010 列表1
    009,分支和循环3
    008,分支和循环2
    006 Python的操作符
    005 Python的数值类型
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4393494.html
Copyright © 2011-2022 走看看