UVA 699 The Falling Leaves

来源:互联网 发布:mybatis 源码 编辑:程序博客网 时间:2024/06/05 20:43

题目链接:http://acm.hust.edu.cn/vjudge/problem/19244



题解:给你一个先序二叉树,其中左子结点在父节点左一个单位,右节点在父节点右一个单位;
按照递归方式输入,-1表示空。让你从左到右输出每个水平位置的权值和。

 不需要真的建树 把当前位置的sum[]加上当前值就行了 


#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <cmath>#include <stack>#include <string>#include <sstream>#include <map>#include <set>#define pi acos(-1.0)#define LL long long#define ULL unsigned long long#define inf 0x3f3f3f3f#define INF 1e18#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define debug(a) printf("---%d---\n", a)#define mem0(a) memset(a, 0, sizeof(a))#define memi(a) memset(a, inf, sizeof(a))#define mem1(a) memset(a, -1, sizeof(a))using namespace std;typedef pair<int, int> P;const double eps = 1e-10;const int maxn = 1e6 + 5;const int mod = 1e8;int sum[maxn];void Build(int pos){    int v;    cin >> v;    if (v == -1) return;    sum[pos] += v;    Build(pos-1);    Build(pos+1);}int main(void){//freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);    int v, cas = 1, pos;    while (cin >> v){        if (v == -1) break;        memset(sum, 0, sizeof(sum));        pos = maxn / 2;     //树根的水平位置        sum[pos] += v;        Build(pos-1);        Build(pos+1);        while (sum[pos]) pos--;// 找最左边的结点         printf("Case %d:\n", cas++);        printf("%d", sum[++pos]);        while (sum[++pos])            printf(" %d", sum[pos]);        puts("\n");    }return 0;}


0 0
原创粉丝点击