This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
struct linkedListNode {
int8_t val;
linkedListNode *next = nullptr;
bool splittable = false;
};
linkedListNode preallocLinked[1000001];
int lastNodeLnkd = 0;
inline 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;
values.push_back(30);
int size = 0;
linkedListNode *head = get_new_lnkd_node();
linkedListNode *tail = head;
stack<int8_t> stck;
stck.push(30);
for (int i = 0; i < N; i++) {
while (stck.top() < values[i]) {
tail->next = get_new_lnkd_node();
tail = tail->next;
tail->val = stck.top();
tail->splittable = (stck.top() != 0);
size++;
stck.pop();
}
while (stck.top() > values[i]) {
int tmp = stck.top()-1;
stck.pop();
stck.push(tmp);
stck.push(tmp);
}
tail->next = get_new_lnkd_node();
tail = tail->next;
tail->val = values[i];
tail->splittable = false;
size++;
stck.pop();
}
while (stck.size()) {
tail->next = get_new_lnkd_node();
tail = tail->next;
tail->val = stck.top();
tail->splittable = true;
size++;
stck.pop();
}
linkedListNode *hd = head->next;
head = head->next;
while (head != nullptr) {
while (size < (N + K) && head->splittable) {
linkedListNode *newNode = get_new_lnkd_node();
head->val = head->val - 1;
head->splittable = (head->val != 0);
newNode->val = head->val;
newNode->next = head->next;
newNode->splittable = (newNode->val != 0);
head->next = newNode;
size++;
}
head = head->next;
}
while (hd != nullptr) {
cout << (int)hd->val << ' ';
hd = hd->next;
}
cout << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |