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
Sample Input
2
1 2 3 4 5 6
Sample Output
HINT
题意
让你从根走到每个叶子所要的花费都相同,请问最少得增加多少边权?
题解:
啊,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("\n");
}
*/
//************************************************************************************** inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
ll powmod(ll a,ll b) {
ll ans=;
for(ll i=;i<=b;i++)
ans*=a;
return ans;
}
ll a[maxn];
ll dp[maxn];
ll n;
void read_dfs(int x)
{
if(x>=powmod(,n))
return;
read_dfs(x*);
read_dfs(x*+);
a[x*]+=max(a[x**],a[x**+]);
a[x*+]+=max(a[(x*+)*],a[(x*+)*+]);
//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(,n))
return ;
ll sum=;
dp[x]=dfs(x*)+dfs(x*+)+abs(a[x*]-a[x*+]);
return dp[x];
}
int main()
{
cin>>n;
int k=powmod(,n);
for(int i=;i<=n;i++)
{
for(int j=;j<powmod(,i);j++)
{
cin>>a[powmod(,i)+j];
}
}
read_dfs();
cout<<dfs()<<endl;
}
ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS的更多相关文章
- ZeptoLab Code Rush 2015 B. Om Nom and Dark Park
Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who l ...
- Codeforces - ZeptoLab Code Rush 2015 - D. Om Nom and Necklace:字符串
D. Om Nom and Necklace time limit per test 1 second memory limit per test 256 megabytes input standa ...
- ZeptoLab Code Rush 2015 C. Om Nom and Candies 暴力
C. Om Nom and Candies Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526 ...
- ZeptoLab Code Rush 2015 C. Om Nom and Candies [ 数学 ]
传送门 C. Om Nom and Candies time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces ZeptoLab Code Rush 2015 D.Om Nom and Necklace(kmp)
题目描述: 有一天,欧姆诺姆发现了一串长度为n的宝石串,上面有五颜六色的宝石.他决定摘取前面若干个宝石来做成一个漂亮的项链. 他对漂亮的项链是这样定义的,现在有一条项链S,当S=A+B+A+B+A+. ...
- CodeForces ZeptoLab Code Rush 2015
拖了好久的题解,想想还是补一下吧. A. King of Thieves 直接枚举起点和5个点之间的间距,进行判断即可. #include <bits/stdc++.h> using na ...
- B. Om Nom and Dark Park
B. Om Nom and Dark Park 在满二叉树上的某些边上添加一些值.使得根节点到叶子节点的路径上的权值和都相等.求最少需要添加多少. 我们利用性质解题. 考察兄弟节点.由于他们从跟节 ...
- Zepto Code Rush 2014 B - Om Nom and Spiders
注意题目给的是一个nxm的park,设元素为aij,元素aij 有4种可能U(上移),D(下移),L(左移),R(右移) 假设第i行第j列元素aij(注意元素的索引是从0开始的) 当aij为D时,此时 ...
- CF Zepto Code Rush 2014 B. Om Nom and Spiders
Om Nom and Spiders time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- Codeforces Round #505
Codeforces Round #505 A. Doggo Recoloring 题目描述:给定一个字符串,每次选择一个在字符串里面出现至少两次的字符,然后将这种字符变成那一种指定的字符,问最终这个 ...
- js如何查看元素类型
<script type="text/javascript"> //定义变量temp var temp = Object.prototype.toString.appl ...
- Windows版Oracle重建EM---备注
前提条件添加环境变量 ORACLE_HOSTNAME=<主机名:如:DESKTOP-P6J1a>ORACLE_SID=orclORACLE_UNQNAME=orcl 执行删除命令 C:\U ...
- 十八、springboot中hibernate配置sessionFactory访问数据库
前提 在yml或properties文件中配置数据库与数据库连接池 Hibernate配置 几种方式: 方式一: @Configuration public class HibernateConfig ...
- No.16 selenium学习之路之异常处理
一.常见的几种异常: SyntaxError:语法错误 NameError:试图访问的变量名不存在 IndexError:索引错误,使用的索引不存在,超出序列范围 KeyError:使用了不存在的关键 ...
- windows下redis服务操作
安装redis服务redis-server --service-install redis.windows.conf --service-name Redis26380 --loglevel verb ...
- 【LOJ】#2289. 「THUWC 2017」在美妙的数学王国中畅游
题解 我们发现,题目告诉我们这个东西就是一个lct 首先,如果只有3,问题就非常简单了,我们算出所有a的总和,所有b的总和就好了 要是1和2也是多项式就好了--其实可以!也就是下面泰勒展开的用处,我们 ...
- oracle中vsize和length
其实LENGTH与VSIZE这两个函数联系不大,区别很大.虽然都是“取长度”,但是LENGTH函数结果是“有多少个字符”,VSIZE结果是“需要多少bytes”.简单看一下这两个函数. 1.创建表T并 ...
- Ionic Js八:头部和底部
1.ion-header-bar 这个是固定在屏幕顶部的一个头部标题栏.如果给它加上'bar-subheader' 这个样式,它就是副标题. <ion-header-bar align-titl ...
- CentOS 配置自启动Redis
第一步: 在/etc/init.d/目录下建立一个名字为 redis 的启动脚本 cd /etc/init.d touch redis 然后在这个脚本中添加如下脚本 <注意修改自己的PIDFI ...