UVa 699 The Falling Leaves(建树+求竖直权值和)
题目链接
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.
1.第一眼看题想到了边建树边操作求每一条竖直线上的和。刚开始想到了数组,又觉得没有map方便,就是可以给根节点的map->first赋值为0,之后递归中每当建左孩子就传下标减一的值,每当建右孩子就传下标加一的值
2.说实话输入真的坑到我了,我说怎么和样例对不上,VJ上的第二行和第三行分开了,实际评测中是算一行的。但奇怪的是LRJ老师的方法即使分行输入也是对的
3.这种输入过程就能出结果的用数组+递归就能解决,没必要再去写结构体
我的代码(130ms)
#include <iostream>
#include <map>
#include <sstream>
#include <cstring>
using namespace std;
const int maxn=1e5+10;
typedef struct node{
int a;
struct node *left,*right;
node():a(0),left(nullptr),right(nullptr){}
}*Node;
Node root;
int order[maxn];
int num,m;
Node newNode(){ return new node(); }
map<int,int> mp;
bool read_line(int *a){
string s;
getline(cin,s);
if(s=="-1") return false;
stringstream ss(s);
int x;
num=0;
while(ss>>x) a[num++]=x;
return num>0;
}
Node BuildTree(int *a,int k){
if(a[m]==-1){
m++;
return nullptr;
}
Node t=newNode();
t->a=a[m++];
if(m>=num) return nullptr;
mp[k]+=t->a;
//cout<<mp[k]<<endl;
t->left=BuildTree(a,k-1);
t->right=BuildTree(a,k+1);
return t;
}
void PreOrderTraverse(Node T) //先序遍历一下树
{
if(T==nullptr)
return;
printf("%d ",T->a);
PreOrderTraverse(T->left);
PreOrderTraverse(T->right);
}
void freeMemory(Node T){
if(T==nullptr)
return;
if(T->left) freeMemory(T->left);
if(T->right) freeMemory(T->right);
delete T;
}
int main()
{
int c=0;
while(read_line(order)){
root=BuildTree(order,0);
if(root==nullptr) continue;
printf("Case %d:\n",++c);
for(map<int,int>::iterator i=mp.begin();i!=mp.end();i++){
if(i!=mp.begin())
printf(" %d",i->second);
else printf("%d",i->second);
}
printf("\n\n");
memset(order,0,sizeof(order));
mp.clear();
m=0;
//PreOrderTraverse(root);
freeMemory(root);
}
return 0;
}
标答:(290ms)
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=1e5+10;
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<<endl<<endl;
}
return 0;
}