#include <bits/stdc++.h>
using namespace std;
struct btNode {
int value;
bool left;
bool right;
};
struct linkedListNode {
int8_t val;
linkedListNode *next = nullptr;
bool inserted = false;
};
linkedListNode preallocLinked[1000001];
int lastNodeLnkd = 0;
static linkedListNode* get_new_lnkd_node() {
return &preallocLinked[lastNodeLnkd++];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, K;
cin >> N >> K;
vector<int> values(N);
for (auto &i: values) cin >> i;
linkedListNode *head = get_new_lnkd_node();
linkedListNode *tail = head;
stack<btNode> stck;
stck.push(btNode{30, 0, 0});
for (int i = 0; i < N; i++) {
while (stck.size() > 1 && (values[i] >= stck.top().value || (stck.top().left && stck.top().right))) {
btNode &top = stck.top();
if (!top.right && top.value != 0) {
tail->next = get_new_lnkd_node();
tail = tail->next;
tail->val = top.value - 1;
if (top.value == 1) tail->inserted = false;
else tail->inserted = true;
top.right = true;
}
stck.pop();
}
tail->next = get_new_lnkd_node();
tail = tail->next;
tail->val = values[i];
tail->inserted = false;
while (stck.top().value != values[i]) {
btNode &top = stck.top();
if (top.left) {
// I have to go right (left completely explored)
btNode right;
right.value = top.value - 1;
top.right = true;
stck.push(right);
} else {
// I can go left
btNode left;
left.value = top.value - 1;
top.left = true;
stck.push(left);
}
}
stck.pop(); // node is == values[i], algorithm would never run next while loop
while (stck.size() > 1 && stck.top().left && stck.top().right) {
btNode &top = stck.top();
if (!top.right && top.value != 0) {
tail->next = get_new_lnkd_node();
tail = tail->next;
tail->val = top.value - 1;
if (top.value == 1) tail->inserted = false;
else tail->inserted = true;
top.right = true;
}
stck.pop();
}
// maybe this works idk
}
while (!stck.empty()) {
btNode top = stck.top();
if (!top.right && top.value != 0) {
tail->next = get_new_lnkd_node();
tail = tail->next;
tail->val = top.value - 1;
if (top.value == 1) tail->inserted = false;
else tail->inserted = true;
}
stck.pop();
}
head = head->next;
while (head != nullptr) {
cout << (int)head->val << ' ';
head = head->next;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
393 ms |
29776 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
419 ms |
29768 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
428 ms |
29764 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
399 ms |
29812 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
392 ms |
29756 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
416 ms |
29764 KB |
Unexpected end of file - int32 expected |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
406 ms |
29772 KB |
Unexpected end of file - int32 expected |
2 |
Incorrect |
405 ms |
29820 KB |
Unexpected end of file - int32 expected |
3 |
Incorrect |
394 ms |
29752 KB |
Unexpected end of file - int32 expected |
4 |
Incorrect |
418 ms |
29768 KB |
Unexpected end of file - int32 expected |
5 |
Incorrect |
413 ms |
29768 KB |
Unexpected end of file - int32 expected |
6 |
Incorrect |
409 ms |
29776 KB |
Unexpected end of file - int32 expected |
7 |
Incorrect |
413 ms |
29764 KB |
Unexpected end of file - int32 expected |
8 |
Incorrect |
404 ms |
29760 KB |
Unexpected end of file - int32 expected |
9 |
Incorrect |
336 ms |
28664 KB |
Unexpected end of file - int32 expected |
10 |
Incorrect |
136 ms |
25484 KB |
Unexpected end of file - int32 expected |
11 |
Incorrect |
201 ms |
26812 KB |
Unexpected end of file - int32 expected |
12 |
Incorrect |
10 ms |
23764 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
10 ms |
23720 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
11 ms |
23708 KB |
Unexpected end of file - int32 expected |