-
Notifications
You must be signed in to change notification settings - Fork 0
/
octopussy.cpp
90 lines (70 loc) · 1.8 KB
/
octopussy.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
87
88
89
90
//
// octopussy.cpp
// Algorithms Lab
//
// Created by Jonas Gessner
// Copyright © 2019 Jonas Gessner. All rights reserved.
//
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void disarmChildTree(const int index, const int n, vector<bool> &disarmed, int *currentTime)
{
if (disarmed.at(index))
{
return;
}
if (index <= ((n - 3) / 2)) // We know that the bomb at index has an earlier explosion time than all of its children. It therefore does not matter in which order we disarm the children.
{
disarmChildTree(2 * index + 1, n, disarmed, currentTime);
disarmChildTree(2 * index + 2, n, disarmed, currentTime);
}
*currentTime = (*currentTime) + 1;
disarmed.at(index) = true;
}
static void runTestCase()
{
int n;
cin >> n;
vector<pair<int, int>> timestamps(n);
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
timestamps.at(i) = {i, x};
}
sort(timestamps.begin(), timestamps.end(), [](const pair<int, int> &lhs, const pair<int, int> &rhs) {
return lhs.second < rhs.second;
});
vector<bool> disarmed(n, false);
int currentTime = 0;
for (const pair<int, int> &entry : timestamps)
{
int timestamp = entry.second;
int index = entry.first;
if (disarmed.at(index))
{
continue;
}
// Disarm entire child tree of ball at index
disarmChildTree(index, n, disarmed, ¤tTime);
if (currentTime > timestamp)
{
cout << "no\n";
return;
}
}
cout << "yes\n";
}
int main(int argc, const char *argv[])
{
ios_base::sync_with_stdio(false);
int t;
cin >> t;
for (int i = 0; i < t; i++)
{
runTestCase();
}
return 0;
}