博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces-526B
阅读量:6880 次
发布时间:2019-06-27

本文共 3021 字,大约阅读时间需要 10 分钟。

题目连接:http://codeforces.com/contest/526/problem/B

B. Om Nom and Dark Park
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
2 1 2 3 4 5 6
output
5
Note

Picture for the sample test. Green color denotes the additional street lights.

题目大意:

给定一个完全二叉树,各边有权值,要求使每颗子树的左右子树边权相同,问最少需要添加多少边权。

解题思路:

利用二叉树的性质,自低向上递推即可,每一层求出各个左右兄弟节点之差(当然求绝对值)求和,每个父亲节点在计算之前加上左右子节点中的最大值,以达到任意子树都相同的目的,推到第一层即可。

代码如下:

#include
using namespace std;int tree[4100]= {
0};int main(){ int n; scanf("%d",&n); int s=1; for(int i=0; i<=n; i++) s*=2; for(int i=2; i<=s-1; i++) scanf("%d",&tree[i]); int ans=0; s/=2; while(s>1) { for(int i=s; i<=s*2-1; i++) tree[i]+=max(tree[i*2],tree[i*2+1]); for(int i=s; i<=s*2-2; i+=2) ans+=abs(tree[i]-tree[i+1]); s/=2; } cout<
<

 

转载于:https://www.cnblogs.com/cryingrain/p/7632990.html

你可能感兴趣的文章
Spring @Value注解使用${}进行注入(转)
查看>>
从HTTL模板引擎看软件设计原则
查看>>
SpringCloud2.0
查看>>
Java 9 逆天的十大新特性
查看>>
加载中提示
查看>>
javascript基础修炼(3)—What's this(下)
查看>>
正则表达式用法简介与速查
查看>>
App 开发:Hybrid 架构下的 HTML5 应用加速方案
查看>>
软件工程综合实践阶段小结
查看>>
Tensorflow学习笔记(1):tf.slice()函数使用
查看>>
ORA-01102的解决办法
查看>>
奇怪的iphone6 plus,H5调用拍照浏览器崩溃
查看>>
MVC接受JSON的一些注意事项
查看>>
response对象设置输出缓冲大小
查看>>
MVC+Ninject+三层架构+代码生成 -- 总结(七、顯示層 一)
查看>>
[CF1105D]Kilani and the Game
查看>>
[bzoj4195][Noi2015]程序自动分析
查看>>
简单的bfs(最短路径)c++
查看>>
Matlab2013a许可证过期问题,反复提示激活
查看>>
向上下左右不间断无缝滚动图片的效果(兼容火狐和IE)
查看>>