제출 #647397

#제출 시각아이디문제언어결과실행 시간메모리
647397AlenygamZalmoxis (BOI18_zalmoxis)C++14
35 / 100
190 ms33040 KiB
#include <bits/stdc++.h>

using namespace std;

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;
	values.push_back(30);

	linkedListNode *head = get_new_lnkd_node();
	linkedListNode *tail = head;

	stack<int> 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->inserted = true;

			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->inserted = false;
		stck.pop();
	}

	while (stck.size()) {
		tail->next = get_new_lnkd_node();
		tail = tail->next;
		tail->val = stck.top();
		tail->inserted = true;
		stck.pop();
	}

	head = head->next;
	while (head != nullptr) {
		cout << (int)head->val << ' ';
		head = head->next;
	}
	cout << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...