当前位置: 首页 > news >正文

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

相关文章:

  • Widget带来了真正的移动互联网
  • 2019 ICPC 徐州区域赛 - C <3 numbers(素数密度)
  • 2019 ICPC 徐州区域赛 - A Cat(异或性质)
  • 2019 ICPC 南昌区域赛 - C And and Pair(思维+组合数学)
  • Android开发指南-框架主题-清单文件
  • 2019 ICPC 南昌区域赛 - G Eating Plan(技巧+暴力)
  • 离职的日子
  • 2019 ICPC 南京区域赛 - A A Hard Problem(找规律)
  • 走向架构师之路---开博寄语
  • JavaWeb拦截器拦截了静态资源的解决办法
  • 职业方向的选择
  • 2019 ICPC 南京区域赛 - H Prince and Princess(博弈+思维)
  • M2M----物联网的未来值得期待
  • 2019 ICPC 徐州区域赛 - E Multiply(Pollard-Rho质因数分解)
  • 备战3G —OMA 协议简介及公共文档下载
  • (三)从jvm层面了解线程的启动和停止
  • css选择器
  • java8-模拟hadoop
  • JavaScript 基本功--面试宝典
  • js作用域和this的理解
  • laravel 用artisan创建自己的模板
  • MySQL QA
  • passportjs 源码分析
  • Redash本地开发环境搭建
  • Webpack 4 学习01(基础配置)
  • 从零开始的webpack生活-0x009:FilesLoader装载文件
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 更好理解的面向对象的Javascript 1 —— 动态类型和多态
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • 力扣(LeetCode)357
  • 我有几个粽子,和一个故事
  • 详解移动APP与web APP的区别
  • 项目实战-Api的解决方案
  • hi-nginx-1.3.4编译安装
  • 如何用纯 CSS 创作一个菱形 loader 动画
  • # 达梦数据库知识点
  • #android不同版本废弃api,新api。
  • #if 1...#endif
  • #Linux(make工具和makefile文件以及makefile语法)
  • ()、[]、{}、(())、[[]]命令替换
  • (1)bark-ml
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (C语言)strcpy与strcpy详解,与模拟实现
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (八)c52学习之旅-中断实验
  • (八)Spring源码解析:Spring MVC
  • (二)Eureka服务搭建,服务注册,服务发现
  • (附源码)node.js知识分享网站 毕业设计 202038
  • (附源码)ssm跨平台教学系统 毕业设计 280843
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理第3章 信息系统治理(一)
  • (三维重建学习)已有位姿放入colmap和3D Gaussian Splatting训练
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • ... 是什么 ?... 有什么用处?
  • .NET Core WebAPI中封装Swagger配置