50105. Seesaw Chandelier Tree

難度:3.7/5

Second Try: 3/5 Used time 17:10

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "construct.h"

// typedef struct Node{
// int value;
// struct Node *left, *right;
// } Node;

int* arr;
int n;

Node *make_left(int l, int r){
Node *head = (Node*) malloc(sizeof(Node));

Node *now = head;
for(int i = r; i >= l; i--){
Node* temp = (Node*) malloc(sizeof(Node));
temp->value = arr[i];
now->left = temp;
now->right = NULL;
now = now->left;
}
now->left = NULL;
now->right = NULL;
return head->left;
}

Node *dfs(int l, int r){
if(r - l < 2) return make_left(l, r);

uint64_t l_bucket = 0, r_bucket = 0;
uint64_t l_weight = 0, r_weight = 0;

uint64_t p = 0;
for(int i = l; i <= r; i++){
r_bucket += arr[i];
r_weight += (p++) * arr[i];
}

for(int i = l; i <= r; i++){
if(l_weight > r_weight) break;
else if(l_weight == r_weight){
Node* torque = (Node*) malloc(sizeof(Node));
torque->value = arr[i];
torque->left = dfs(l, i - 1);
torque->right = dfs(i + 1, r);
return torque;
}
else{
l_bucket += (uint64_t)arr[i], r_bucket -= (uint64_t)arr[i];
l_weight += l_bucket, r_weight -= r_bucket;
}
}

return make_left(l, r);
}

Node *ConstructTree(int T[], int N){
arr = T;
n = N;
return dfs(0, n - 1);
}


50105. Seesaw Chandelier Tree
https://aaronlin1229.github.io/judgegirl_50105/
Author
Akizumi
Posted on
July 17, 2023
Licensed under