日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【两种解法】he Falling Leaves UVA - 699

發布時間:2024/2/28 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【两种解法】he Falling Leaves UVA - 699 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

立志用最少的代碼做最高效的表達


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; }

擇苦而安,擇做而樂,虛擬終究不比真實精彩之萬一。

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的【两种解法】he Falling Leaves UVA - 699的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。