Initial commit
This commit is contained in:
131
2022/republic/12/arbore/arbore.cpp
Normal file
131
2022/republic/12/arbore/arbore.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Node {
|
||||
Node *left;
|
||||
Node *right;
|
||||
int64_t height;
|
||||
};
|
||||
|
||||
|
||||
void createNewNode(Node &parent, list<Node> &freeNodesLeft, list<Node> &freeNodesRight, int64_t new_height, bool direction) {
|
||||
// Found a node that will not increase total height. Using
|
||||
Node newNode;
|
||||
newNode.height = new_height;
|
||||
|
||||
freeNodesLeft.push_back(newNode);
|
||||
freeNodesRight.push_back(newNode);
|
||||
|
||||
if(!direction) {
|
||||
parent.left = &newNode;
|
||||
} else {
|
||||
parent.right = &newNode;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int64_t calculateHeight(int64_t N, int64_t leftHeight, int64_t rightHeight) {
|
||||
if (N == 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t totalHeight = 0;
|
||||
Node parentNode;
|
||||
parentNode.height = 0;
|
||||
|
||||
list<Node> freeNodesLeft;
|
||||
list<Node> freeNodesRight;
|
||||
|
||||
freeNodesLeft.push_back(parentNode);
|
||||
freeNodesRight.push_back(parentNode);
|
||||
|
||||
int64_t longestRoad = max(leftHeight, rightHeight);
|
||||
|
||||
// Calculate all houses after first one
|
||||
for (int64_t i = 1; i < N; i++) {
|
||||
Node lowestNodeLeft;
|
||||
lowestNodeLeft.height = longestRoad * N;
|
||||
auto lowestNodeLeftIt = freeNodesLeft.end();
|
||||
|
||||
bool foundFreeSolution = false;
|
||||
|
||||
// Find lowest left node. Also check if using any of the existing nodes will not increase total height
|
||||
for (auto it = freeNodesLeft.begin(); it != freeNodesLeft.end(); it++) {
|
||||
Node node = *it;
|
||||
int64_t new_height = node.height + leftHeight;
|
||||
|
||||
if (new_height <= totalHeight) {
|
||||
// Found a node that will not increase total height. Using it
|
||||
createNewNode(node, freeNodesLeft, freeNodesRight, new_height, 0);
|
||||
freeNodesLeft.erase(it);
|
||||
foundFreeSolution = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (new_height < lowestNodeLeft.height) {
|
||||
lowestNodeLeft = node;
|
||||
lowestNodeLeftIt = it;
|
||||
}
|
||||
}
|
||||
|
||||
if (foundFreeSolution) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Node lowestNodeRight;
|
||||
lowestNodeRight.height = longestRoad * N;
|
||||
auto lowestNodeRightIt = freeNodesRight.end();
|
||||
|
||||
// Find lowest right node. Also check if using any of the existing nodes will not increase total height
|
||||
for (auto it = freeNodesRight.begin(); it != freeNodesRight.end(); it++) {
|
||||
Node node = *it;
|
||||
int64_t new_height = node.height + rightHeight;
|
||||
|
||||
if (new_height <= totalHeight) {
|
||||
// Found a node that will not increase total height. Using
|
||||
createNewNode(node, freeNodesLeft, freeNodesRight, new_height, 1);
|
||||
freeNodesRight.erase(it);
|
||||
foundFreeSolution = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (new_height < lowestNodeRight.height) {
|
||||
lowestNodeRight = node;
|
||||
lowestNodeRightIt = it;
|
||||
}
|
||||
}
|
||||
|
||||
if (foundFreeSolution) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int64_t newHeightLeft = lowestNodeLeft.height + leftHeight;
|
||||
int64_t newHeightRight = lowestNodeRight.height + rightHeight;
|
||||
if (newHeightLeft < newHeightRight) {
|
||||
createNewNode(lowestNodeLeft, freeNodesLeft, freeNodesRight, newHeightLeft, 0);
|
||||
freeNodesLeft.erase(lowestNodeLeftIt);
|
||||
totalHeight = newHeightLeft;
|
||||
} else {
|
||||
createNewNode(lowestNodeRight, freeNodesLeft, freeNodesRight, newHeightRight, 1);
|
||||
freeNodesRight.erase(lowestNodeRightIt);
|
||||
totalHeight = newHeightRight;
|
||||
}
|
||||
//cout << "i=" << i << "; total_height=" << totalHeight << endl;
|
||||
}
|
||||
|
||||
return totalHeight;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int64_t T;
|
||||
cin >> T;
|
||||
|
||||
int64_t N, A, B;
|
||||
for (int i = 0; i < T; i++) {
|
||||
cin >> N >> A >> B;
|
||||
cout << calculateHeight(N, A, B) << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
6
2022/republic/12/arbore/tests/input.01
Normal file
6
2022/republic/12/arbore/tests/input.01
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
7 3 4
|
||||
20 15 3
|
||||
46 28 46
|
||||
79 73 27
|
||||
97 97 8
|
||||
6
2022/republic/12/arbore/tests/input.02
Normal file
6
2022/republic/12/arbore/tests/input.02
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
10 4 2
|
||||
16 8 2
|
||||
53 23 50
|
||||
77 79 21
|
||||
98 95 2
|
||||
6
2022/republic/12/arbore/tests/input.03
Normal file
6
2022/republic/12/arbore/tests/input.03
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
99965 35 53
|
||||
99952 109 999999959
|
||||
44 999999970 56
|
||||
99953 999999977 10000006
|
||||
9968 1623 6933
|
||||
6
2022/republic/12/arbore/tests/input.04
Normal file
6
2022/republic/12/arbore/tests/input.04
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
99961 32 7
|
||||
99984 125 999999963
|
||||
6 999999982 84
|
||||
99960 999999969 10000008
|
||||
9977 1642 6947
|
||||
6
2022/republic/12/arbore/tests/input.05
Normal file
6
2022/republic/12/arbore/tests/input.05
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
999957 17 34
|
||||
921381 87 999999958
|
||||
523449 999999953 11
|
||||
999965 999999983 10000031
|
||||
99959 1594 6926
|
||||
6
2022/republic/12/arbore/tests/input.06
Normal file
6
2022/republic/12/arbore/tests/input.06
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
999956 48 49
|
||||
921389 112 999999963
|
||||
523516 999999982 23
|
||||
999955 999999995 10000004
|
||||
100000 1575 6937
|
||||
6
2022/republic/12/arbore/tests/input.07
Normal file
6
2022/republic/12/arbore/tests/input.07
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
912381233 32 35
|
||||
921381011 8 9960
|
||||
77 9495 5693
|
||||
68138568 9950 588
|
||||
100000009 1570 6910
|
||||
6
2022/republic/12/arbore/tests/input.08
Normal file
6
2022/republic/12/arbore/tests/input.08
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
912381240 12 3
|
||||
921380990 50 9993
|
||||
143 9466 5680
|
||||
68138571 9970 555
|
||||
99999959 1612 6932
|
||||
6
2022/republic/12/arbore/tests/input.09
Normal file
6
2022/republic/12/arbore/tests/input.09
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
912381235 26 42
|
||||
921381037 56 9967
|
||||
119 9476 5670
|
||||
68138534 9969 596
|
||||
99999980 1565 6886
|
||||
6
2022/republic/12/arbore/tests/input.10
Normal file
6
2022/republic/12/arbore/tests/input.10
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
912381244 32 16
|
||||
921381100 184 999999967
|
||||
723548188 9464 567100060
|
||||
68138629 9979 611
|
||||
100000029 77 692299979
|
||||
6
2022/republic/12/arbore/tests/input.11
Normal file
6
2022/republic/12/arbore/tests/input.11
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
912381201 16 18
|
||||
921380950 49 999999927
|
||||
723548029 9454 567099988
|
||||
68138493 10021 572
|
||||
100000008 108 692299958
|
||||
6
2022/republic/12/arbore/tests/input.12
Normal file
6
2022/republic/12/arbore/tests/input.12
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
912381177 37 87
|
||||
921380926 143 999999963
|
||||
723548128 9443 567099903
|
||||
68138471 10046 648
|
||||
100000099 1 692300051
|
||||
6
2022/republic/12/arbore/tests/input.13
Normal file
6
2022/republic/12/arbore/tests/input.13
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
999999988 523123166 512342176
|
||||
999999997 912344119 912344027
|
||||
500000056 5581 8739
|
||||
699999901 36 123091156
|
||||
999999962 9232 123412305
|
||||
6
2022/republic/12/arbore/tests/input.14
Normal file
6
2022/republic/12/arbore/tests/input.14
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
999999985 523123188 512342122
|
||||
999999909 912344010 912344072
|
||||
500000037 5391 8776
|
||||
700000093 23 123091331
|
||||
999999952 9219 123412364
|
||||
6
2022/republic/12/arbore/tests/input.15
Normal file
6
2022/republic/12/arbore/tests/input.15
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
999999923 523123196 512342231
|
||||
999999982 912343949 912343923
|
||||
499999995 5562 8755
|
||||
699999928 76 123091321
|
||||
999999941 9308 123412406
|
||||
6
2022/republic/12/arbore/tests/input.16
Normal file
6
2022/republic/12/arbore/tests/input.16
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
999999939 523123090 512342081
|
||||
999999908 912344098 912343994
|
||||
500000007 5481 8718
|
||||
700000015 86 123091188
|
||||
999999988 9253 123412300
|
||||
6
2022/republic/12/arbore/tests/input.17
Normal file
6
2022/republic/12/arbore/tests/input.17
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
999999959 523123094 512342066
|
||||
999999978 912344083 912344114
|
||||
500000003 5467 8715
|
||||
699999929 41 123091277
|
||||
999999937 9174 123412394
|
||||
6
2022/republic/12/arbore/tests/input.18
Normal file
6
2022/republic/12/arbore/tests/input.18
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
999999943 523123145 512342179
|
||||
999999923 912344071 912343968
|
||||
499999934 5569 8762
|
||||
699999982 111 123091195
|
||||
999999989 9325 123412428
|
||||
6
2022/republic/12/arbore/tests/input.19
Normal file
6
2022/republic/12/arbore/tests/input.19
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
999999943 523123161 512342155
|
||||
999999910 912344077 912344092
|
||||
500000042 5413 8688
|
||||
699999902 17 123091318
|
||||
999999962 9149 123412312
|
||||
6
2022/republic/12/arbore/tests/input.20
Normal file
6
2022/republic/12/arbore/tests/input.20
Normal file
@@ -0,0 +1,6 @@
|
||||
5
|
||||
999999900 523123040 512342210
|
||||
999999982 912344006 912344042
|
||||
499999903 5418 8745
|
||||
700000039 52 123091277
|
||||
999999998 9208 123412250
|
||||
5
2022/republic/12/arbore/tests/output.01
Normal file
5
2022/republic/12/arbore/tests/output.01
Normal file
@@ -0,0 +1,5 @@
|
||||
8
|
||||
27
|
||||
176
|
||||
262
|
||||
185
|
||||
5
2022/republic/12/arbore/tests/output.02
Normal file
5
2022/republic/12/arbore/tests/output.02
Normal file
@@ -0,0 +1,5 @@
|
||||
8
|
||||
14
|
||||
173
|
||||
242
|
||||
111
|
||||
5
2022/republic/12/arbore/tests/output.03
Normal file
5
2022/republic/12/arbore/tests/output.03
Normal file
@@ -0,0 +1,5 @@
|
||||
702
|
||||
10894659
|
||||
2408
|
||||
2780000422
|
||||
45585
|
||||
5
2022/republic/12/arbore/tests/output.04
Normal file
5
2022/republic/12/arbore/tests/output.04
Normal file
@@ -0,0 +1,5 @@
|
||||
258
|
||||
12497875
|
||||
420
|
||||
2780000562
|
||||
45850
|
||||
5
2022/republic/12/arbore/tests/output.05
Normal file
5
2022/republic/12/arbore/tests/output.05
Normal file
@@ -0,0 +1,5 @@
|
||||
476
|
||||
80160060
|
||||
5757928
|
||||
3520004678
|
||||
56946
|
||||
5
2022/republic/12/arbore/tests/output.06
Normal file
5
2022/republic/12/arbore/tests/output.06
Normal file
@@ -0,0 +1,5 @@
|
||||
924
|
||||
103195456
|
||||
12040845
|
||||
3520000598
|
||||
56735
|
||||
5
2022/republic/12/arbore/tests/output.07
Normal file
5
2022/republic/12/arbore/tests/output.07
Normal file
@@ -0,0 +1,5 @@
|
||||
976
|
||||
32216
|
||||
41762
|
||||
79196
|
||||
92330
|
||||
5
2022/republic/12/arbore/tests/output.08
Normal file
5
2022/republic/12/arbore/tests/output.08
Normal file
@@ -0,0 +1,5 @@
|
||||
186
|
||||
46622
|
||||
49224
|
||||
78135
|
||||
93500
|
||||
5
2022/republic/12/arbore/tests/output.09
Normal file
5
2022/republic/12/arbore/tests/output.09
Normal file
@@ -0,0 +1,5 @@
|
||||
972
|
||||
47652
|
||||
47302
|
||||
79808
|
||||
92022
|
||||
5
2022/republic/12/arbore/tests/output.10
Normal file
5
2022/republic/12/arbore/tests/output.10
Normal file
@@ -0,0 +1,5 @@
|
||||
672
|
||||
1007874983
|
||||
927082228
|
||||
80648
|
||||
693338632
|
||||
5
2022/republic/12/arbore/tests/output.11
Normal file
5
2022/republic/12/arbore/tests/output.11
Normal file
@@ -0,0 +1,5 @@
|
||||
496
|
||||
1002079830
|
||||
926701786
|
||||
79002
|
||||
693777290
|
||||
5
2022/republic/12/arbore/tests/output.12
Normal file
5
2022/republic/12/arbore/tests/output.12
Normal file
@@ -0,0 +1,5 @@
|
||||
1697
|
||||
1006114929
|
||||
926283294
|
||||
82630
|
||||
100000098
|
||||
5
2022/republic/12/arbore/tests/output.13
Normal file
5
2022/republic/12/arbore/tests/output.13
Normal file
@@ -0,0 +1,5 @@
|
||||
15041199934
|
||||
26457978347
|
||||
199745
|
||||
124434784
|
||||
262925218
|
||||
5
2022/republic/12/arbore/tests/output.14
Normal file
5
2022/republic/12/arbore/tests/output.14
Normal file
@@ -0,0 +1,5 @@
|
||||
15041199660
|
||||
26457977344
|
||||
196959
|
||||
123948564
|
||||
262893445
|
||||
5
2022/republic/12/arbore/tests/output.15
Normal file
5
2022/republic/12/arbore/tests/output.15
Normal file
@@ -0,0 +1,5 @@
|
||||
15041201104
|
||||
26457974209
|
||||
199614
|
||||
125931517
|
||||
263067272
|
||||
5
2022/republic/12/arbore/tests/output.16
Normal file
5
2022/republic/12/arbore/tests/output.16
Normal file
@@ -0,0 +1,5 @@
|
||||
15041197502
|
||||
26457977594
|
||||
197793
|
||||
126305524
|
||||
262961832
|
||||
5
2022/republic/12/arbore/tests/output.17
Normal file
5
2022/republic/12/arbore/tests/output.17
Normal file
@@ -0,0 +1,5 @@
|
||||
15041197390
|
||||
26457978934
|
||||
197519
|
||||
124621971
|
||||
262815070
|
||||
5
2022/republic/12/arbore/tests/output.18
Normal file
5
2022/republic/12/arbore/tests/output.18
Normal file
@@ -0,0 +1,5 @@
|
||||
15041199613
|
||||
26457976823
|
||||
199817
|
||||
127240930
|
||||
263096981
|
||||
5
2022/republic/12/arbore/tests/output.19
Normal file
5
2022/republic/12/arbore/tests/output.19
Normal file
@@ -0,0 +1,5 @@
|
||||
15041199597
|
||||
26457978488
|
||||
196277
|
||||
123724075
|
||||
262762182
|
||||
5
2022/republic/12/arbore/tests/output.20
Normal file
5
2022/republic/12/arbore/tests/output.20
Normal file
@@ -0,0 +1,5 @@
|
||||
15041198200
|
||||
26457976786
|
||||
197046
|
||||
125033529
|
||||
262874044
|
||||
Reference in New Issue
Block a user