-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
86 lines (69 loc) · 1.7 KB
/
main.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
struct Bomb {
int detTime;
int index;
};
void testcase() {
int n;
std::cin >> n;
std::vector<Bomb> bombs(n);
for (int i = 0; i < n; i++) {
int detTime;
std::cin >> detTime;
bombs[i] = {detTime, i};
}
int firstGrounded = (n - 1) / 2;
for (int i = 0; i < firstGrounded; i++) {
bombs[2 * i + 1].detTime =
std::min(bombs[i].detTime - 1, bombs[2 * i + 1].detTime);
bombs[2 * i + 2].detTime =
std::min(bombs[i].detTime - 1, bombs[2 * i + 2].detTime);
}
auto comparator = [](const Bomb &a, const Bomb &b) {
return a.detTime > b.detTime;
};
std::priority_queue<Bomb, std::vector<Bomb>, decltype(comparator)> prioQueue(
comparator);
for (int i = firstGrounded; i < n; i++) {
prioQueue.push(bombs[i]);
}
int currentTime = 1;
std::set<int> popped;
while (!prioQueue.empty()) {
Bomb top = prioQueue.top();
prioQueue.pop();
if (top.detTime < currentTime) {
std::cout << "no\n";
return;
}
popped.insert(top.index);
// Get the parent of the current node (if possible)
int secondChild;
int parentIndex;
if (top.index % 2 == 0) {
parentIndex = (top.index / 2) - 1;
secondChild = 2 * parentIndex + 1;
} else {
parentIndex = ((top.index + 1) / 2) - 1;
secondChild = 2 * parentIndex + 2;
}
if (popped.find(secondChild) != popped.end()) {
prioQueue.push(bombs[parentIndex]);
}
currentTime++;
}
std::cout << "yes\n";
}
int main() {
std::ios_base::sync_with_stdio(false);
int t;
std::cin >> t;
for (int i = 0; i < t; i++) {
testcase();
}
return 0;
}