日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2024/2/28 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【两种解法】he Falling Leaves UVA - 699 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

立志用最少的代碼做最高效的表達(dá)


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 “l(fā)eaves” 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


分析

本題核心思想就是隱式建樹(shù)。 通過(guò)先序遍歷遞歸,同時(shí)建立map映射或數(shù)組進(jìn)行統(tǒng)計(jì)即可。


解法一:map解法

耗時(shí):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); //初始化,隱式建樹(shù)if(cnt.empty()) break; //如果是空的則結(jié)束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; }

解法二:數(shù)組解法

耗時(shí):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; //樹(shù)根的水平位置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++]; //因?yàn)橐苊庑心┒嘤嗫崭?/span>while(sum[p] != 0) cout << " " << sum[p++];cout << "\n\n";}return 0; }

擇苦而安,擇做而樂(lè),虛擬終究不比真實(shí)精彩之萬(wàn)一。

超強(qiáng)干貨來(lái)襲 云風(fēng)專(zhuān)訪(fǎng):近40年碼齡,通宵達(dá)旦的技術(shù)人生

總結(jié)

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

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。