ZeptoLab Code Rush 2015 B. Om Nom and Dark Park

1.题目描述:点击打开链接

2.解题思路:比赛时候这道题没有做出来,第二天早晨补题时才发现就是简单的DFS应用。题目要求出最少需要增加几盏路灯。假设我们已经知道了root的左子结点一共有suml盏路灯,右子结点一共有sumr盏路灯,那么比较一下d[lson(root)]+suml和d[rson(root)]+sumr的大小即可。此时需要增加的路灯数量就是两者差的绝对值。同时返回较大的数即得到root的总路灯数量。

3.代码:

#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 12
int d[1 << N];
int n;
int cnt;
int dfs(int root)//返回root的路灯总数量
{
	int l = 0, r = 0;
	if (d[lson(root)])l=dfs(lson(root));
	if (d[rson(root)])r=dfs(rson(root));
	if (!d[lson(root)] && !d[rson(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;
}
时间: 2025-01-02 18:45:22

ZeptoLab Code Rush 2015 B. Om Nom and Dark Park的相关文章

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 a

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/problem/C Description A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him

ZeptoLab Code Rush 2015 C. Om Nom and Candies

1.题目描述:点击打开链接 2.解题思路:本题是无限背包问题,根据重量的约束关系,直接暴力搜索. 3.代码: #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<string> #include<sstream> #include<set> #include<vector> #include<stack> #incl

Codeforces ZeptoLab Code Rush 2015

比赛链接:http://codeforces.com/contest/526/ A. King of Thieves time limit per test:1 second memory limit per test:256 megabytes In this problem you will meet the simplified model of game King of Thieves. In a new ZeptoLab game called "King of Thieves&quo

【codeforces ZeptoLab Code Rush 2015】ABCD题解

A. King of Thieves time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output In this problem you will meet the simplified model of game King of Thieves. In a new ZeptoLab game called "King of Thieves&quo

B. Om Nom and Dark Park

B. Om Nom and Dark Park 在满二叉树上的某些边上添加一些值.使得根节点到叶子节点的路径上的权值和都相等.求最少需要添加多少. 我们利用性质解题.   考察兄弟节点.由于他们从跟节点到父节点这路径是相同的,所以需要添加的值为  2*max(lch,rch)-lch-rch;  同时将max(lch,rch)累加到父节点. 思路是最重要的.有时候不是聪不聪明的问题,而是会不会思考的问题. 1 #include <iostream> 2 #include <cstdio&

ZeptoLab Code Rush 2015 A. King of Thieves 暴力

A. King of Thieves Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526/problem/A Description In this problem you will meet the simplified model of game King of Thieves. In a new ZeptoLab game called "King of Thieves" y

ZeptoLab Code Rush 2015

A 题意:给出一串由.*组成的字符串,如果有等间距的五个及五个以上的*存在,则输出yes 直接枚举就可以了 看题一定要仔细啊,做的时候看成必须有五个等间距的".*"才可以跳跃= = 然后就这样写居然过了预测= =后来果然被hack了 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6

ZeptoLab Code Rush 2015 A. King of Thieves

题目大意: 就是说,对于一个起点,使得从这个起点开始,每次间隔相同的格子后,所经过的地方都是‘*’ 解题思路: 直接暴力,枚举起点+枚举公差 代码: 1 # include<cstdio> 2 # include<iostream> 3 4 using namespace std; 5 6 # define MAX 123 7 8 char s[MAX]; 9 int a[MAX]; 10 11 int main(void) 12 { 13 int n; 14 scanf(&quo