立志用最少的代码做最高效的表达


Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how large would the piles of leaves become?
We assume each node in a binary tree ”drops” a number of leaves equal to the integer value stored in that node. We also assume that these leaves drop vertically to the ground (thankfully, there’s no wind to blow them around). Finally, we assume that the nodes are positioned horizontally in such a manner that the left and right children of a node are exactly one unit to the left and one unit to the right, respectively, of their parent. Consider the following tree on the right: The nodes containing 5 and 6 have the same horizontal position (with different vertical positions, of course). The node containing 7 is one unit to the left of those containing 5 and 6, and the node containing 3 is one unit to their right. When the ”leaves” drop from these nodes, three piles are created: the leftmost one contains 7 leaves (from the leftmost node), the next contains 11 (from the nodes containing 5 and 6), and the rightmost pile contains 3. (While it is true that only leaf nodes in a tree would logically have leaves, we ignore that in this problem.)

Input
The input contains multiple test cases, each describing a single tree. A tree is specified by giving the value in the root node, followed by the description of the left subtree, and then the description of the right subtree. If a subtree is empty, the value ‘-1’ is supplied. Thus the tree shown above is specified as ‘5 7 -1 6 -1 -1 3 -1 -1’. Each actual tree node contains a positive, non-zero value. The last test case is followed by a single ‘-1’ (which would otherwise represent an empty tree).

Output
For each test case, display the case number (they are numbered sequentially, starting with 1) on a line by itself. On the next line display the number of “leaves” in each pile, from left to right, with a single space separating each value. This display must start in column 1, and will not exceed the width of an 80-character line. Follow the output for each case by a blank line. This format is illustrated in the
examples below.

Sample Input
5 7 -1 6 -1 -1 3 -1 -1
8 2 9 -1 -1 6 5 -1 -1 12 -1
-1 3 7 -1 -1 -1
-1
Sample Output
Case 1:
7 11 3
Case 2:
9 7 21 15


分析

本题核心思想就是隐式建树。 通过先序遍历递归,同时建立map映射或数组进行统计即可。


解法一:map解法

耗时:40ms

#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
map<int, int>cnt;void createTree(int pos = 0) {int v; cin >> v;if(v != -1) {cnt[pos] += v;createTree(pos-1);createTree(pos+1); }
}int main() {for(int i = 1; ; i++) {cnt.clear(); createTree(0);      //初始化,隐式建树if(cnt.empty()) break;         //如果是空的则结束printf("Case %d:\n", i);for(auto p : cnt) {         //rbegin()配合auto printf("%d%s", p.second, p.first==cnt.rbegin()->first ? "\n" : " "); } putchar('\n');}return 0;
}

解法二:数组解法

耗时:30ms

#include<bits/stdc++.h>
using namespace std;const int maxn = 100;
int sum[maxn];void build(int p) {int v; cin >> v;if(v == -1) return;sum[p] += v;build(p-1); build(p+1);
}bool init() {int v; cin >> v;if(v == -1) return false;memset(sum, 0, sizeof(sum));int pos = maxn / 2;             //树根的水平位置sum[pos] = v;build(pos-1);    build(pos+1); return true;
}int main() {int kase = 0;while(init()) {int p = 0;while(sum[p] == 0) p++;        //找最左边的叶子cout << "Case " << ++kase << ":\n" << sum[p++];    //因为要避免行末多余空格while(sum[p] != 0) cout << " " << sum[p++];cout << "\n\n";}return 0;
}

择苦而安,择做而乐,虚拟终究不比真实精彩之万一。

【两种解法】he Falling Leaves UVA - 699相关推荐

  1. The Falling Leaves UVA - 699

    题目链接:https://vjudge.net/problem/UVA-699 题目大意:给一颗二叉树,每个结点都有一个水平位置 :左子节点在它左边的1个单位,右子结点在它右边1个单位.从左向右输出每 ...

  2. Leetcode 855. Exam Room 考场就座:提供两种解法

    Leetcode 855. Exam Room 考场就座: 提供两种解法 855. Exam Room 考场就座(两种解法) 题目描述 示例: 解答1 代码1 解答2 代码2 855. Exam Ro ...

  3. usaco Ordered Fractions 顺序的分数(两种解法)

    这题很好玩,这里有两种解法. 第一种我自己写的,先找到所有的既约真分数,然后写了一个cmp函数进行排序最后输出.我写的时候还在想这章不是搜索吗这跟搜索关系不大吧,难道是怕我们思维定式化故意出的题不是搜 ...

  4. 约瑟夫环问题的两种解法(详解)

    约瑟夫环问题的两种解法(详解) 题目: Josephus有过的故事:39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓.于是决定了自杀方式,41个人排成一个圆 ...

  5. 牛客--追债之旅 两种解法

    文章目录 第一种 第二种: 一共两种解法,所以即便你不会最短路,也可以做,甚至爆搜+剪枝的时间和空间消耗小于最短路做法. 第一种 题意: 小明现在要追讨一笔债务,已知有n座城市,每个城市都有编号,城市 ...

  6. leetcode 73 矩阵置零 C++ 两种解法

    leetcode 73 两种解法~~,没有一个是我想出来的,哈哈~~ one class Solution {public:void setZeroes(vector<vector<int ...

  7. 北林oj-算法设计与分析-Line up in the canteen(两种解法,附思路)

    描述 One day, there is a kind of new delicious food from one of the windows in the canteen. All studen ...

  8. 洛谷——P1597 语句解析(两种解法)

    P1597 语句解析(两种解法) 题目背景 木有背景-- 题目描述 一串长度不超过 255 的 PASCAL 语言代码,只有 a,b,c 3 个变量,而且只有赋值语句,赋值只能是一个一位的数字或一个变 ...

  9. 整数拆分的两种解法(已完成)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 整数拆分 ...

最新文章

  1. Linux那些事儿之我是Sysfs(2)linux设备底层模型
  2. dos 下启动mysql时,报服务器找不到错误
  3. python随机数程序源码_Python 实现随机数详解及实例代码
  4. 实验八 分析一个奇怪的程序
  5. antd的table进行列筛选时,更新dataSource,为什么table显示暂无数据?
  6. 【渝粤题库】广东开放大学 质量管理 形成性考核 (2)
  7. 【转】2.1(译)关于async与await的FAQ
  8. golang切片类型
  9. SAP License:O2O模式的衡量标准是什么?
  10. 【代码笔记】iOS-判断中英文混合的字符长度的两种方法
  11. Java开发笔记(一百四十八)通过JDBC查询数据记录
  12. 凯撒密码(移位密码)
  13. 北京师范大学网络教育计算机考试题,2020年北京师范大学网络教育入学考试高起专语文模拟题及答案(机考古文阅读)...
  14. Java 将两个日期的时间段按照一定天数进行周期切割
  15. 3月18日面试题复盘
  16. win10开机桌面壁纸位置
  17. Gox语言——支持跨平台原生GUI开发的轻量级全功能脚本语言 - GX1
  18. Java中的集合多线程的理解
  19. UE4地编基础-灯光篇
  20. java 发送 mail 纯文本发送和html格式发送

热门文章

  1. 2021年,腾讯研发人员增长41%,Go首次超越C++成为最热门语言
  2. ETCD 问题、调优、监控
  3. Amazon上最畅销的「操作系统书」有哪些?
  4. 06 / LiveVideoStack主编观察:六岁的Frame.io被收购
  5. 【突破移动端性能极限】
  6. Google Duo采用WaveNetEQ填补语音间隙
  7. 语音编解码技术演进和应用选型
  8. 深度召回模型在QQ看点推荐中的应用实践
  9. SRS性能、内存优化工具用法
  10. JAVA程序设计----多线程(上)