[ZeptoLab Code Rush 2015]Om Nom and Dark Park

本文探讨了一颗满二叉树背景下,如何通过最小化额外光线的安装数量,确保从入口到出口的每条路径拥有相同的光线数量,以确保主角OmNom的安全通行。通过贪心算法和递归策略,实现最优解,算法效率为O(n),适用于满二叉树结构的路径优化问题。

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 test(s)

input
2
1 2 3 4 5 6

output
5

Note

Picture for the sample test. Green color denotes the additional street lights.
这里写图片描述

Standard Report

We use greedy and recursion to solve this task. For each tree rooted at v, we adjust its two subtrees at first, using recursion. Then we increase one edge from v’s child to v.

My Problem Report

这道题是一颗满二叉树,题目要求使根节点到最底层节点的每一条路径的权值都相同时,所需添加在任意边上的最小权值总和。
满二叉树的性质在算法导论上关于二叉堆的章节中有详细阐述。我在思考这种题的时候是受了算导上利用一维数组来静态储存二叉树的启发(这道题中也有足够的提示了)。
首先我们从局部开始思考,对于某个节点,我们可以将它视作一个根节点,那么它通向左边任意一个最底层节点路径的边权必须与右边相同,因为我们无法修改通过比它更高的边来达到这个条件。
那么,对于一个节点,如果说它的左孩子和右孩子都已经完成了修改,我们可以将它的左子树和右子树分别当作一个结点来看待,当前我们就只需使该父亲节点左边权和右边权相等。同理如果我们要是总的修改最少的话,也只需修改max(left,right)-min(left,right)次。
因此我们得出一个结论:当更矮的边完成修改时,我们才能修改更高的边。于是结合满二叉树的构造性质,很容易可以想到反过来遍历一次我们储存二叉树的数组。所以这个算法的效率是 O(n) 。

Source Code

//  Created by Chlerry in 2015.
//  Copyright (c) 2015 Chlerry. All rights reserved.
//  http://codeforces.com/problemset/problem/526/B

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <map>
using namespace std;

int h,n;
long long a[10000],ans;

long long Max(long long x,long long y)
{
    return x>y?x:y;
}

int main()
{
    //freopen("in2.txt","r",stdin);
    cin>>h;
    n=(2<<h)-1;
    for(int i=2;i<=n;i++)
        cin>>a[i];
    for(int i=n;i>0;i--)
    {
        long long temp=Max(a[i*2],a[i*2+1]);
        a[i]+=temp;
        ans+=temp*2-a[i*2]-a[i*2+1];
    }
    cout<<ans<<endl;
    return 0;
}
【SCI复现】基于纳什博弈的多微网主体电热双层共享策略研究(Matlab代码实现)内容概要:本文围绕“基于纳什博弈的多微网主体电热双层共享策略研究”展开,结合Matlab代码实现,复现了SCI级别的科研成果。研究聚焦于多个微网主体之间的能源共享问题,引入纳什博弈理论构建双层优化模型,上层为各微网间的非合作博弈策略,下层为各微网内部电热联合优化调度,实现能源高效利用与经济性目标的平衡。文中详细阐述了模型构建、博弈均衡求解、约束处理及算法实现过程,并通过Matlab编程进行仿真验证,展示了多微网在电热耦合条件下的运行特性和共享效益。; 适合人群:具备一定电力系统、优化理论和博弈论基础知识的研究生、科研人员及从事能源互联网、微电网优化等相关领域的工程师。; 使用场景及目标:① 学习如何将纳什博弈应用于多主体能源系统优化;② 掌握双层优化模型的建模与求解方法;③ 复现SCI论文中的仿真案例,提升科研实践能力;④ 为微电网集群协同调度、能源共享机制设计提供技术参考。; 阅读建议:建议读者结合Matlab代码逐行理解模型实现细节,重点关注博弈均衡的求解过程与双层结构的迭代逻辑,同时可尝试修改参数或扩展模型以适应不同应用场景,深化对多主体协同优化机制的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值