-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathF-Stack sort.h
56 lines (52 loc) · 1.06 KB
/
F-Stack sort.h
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
#pragma once
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <cmath>
#include <string>
#include <stack>
#include <deque>
#include <iomanip>
#include <numeric>
#include <queue>
#include <list>
void compound() {
int n;
std::cin >> n;
std::stack<int> s;
std::vector<int> v(n);
std::vector<std::string> words;
for (int i = 0; i < n; i++) {
std::cin >> v[i];
}
int last = 0;
for (int i = 0; i < n; i++) {
if (s.empty() || (v[i] < s.top() && last < v[i])) {
s.push(v[i]);
words.push_back("push");
}
else if (v[i] < last && v[i] < s.top()) {
std::cout << "impossible" << std::endl;
return;
}
else if (v[i] > s.top()) {
while (!s.empty() && v[i] > s.top()) {
last = s.top();
s.pop();
words.push_back("pop");
}
s.push(v[i]);
words.push_back("push");
}
if (i == n - 1 && !s.empty()) {
while (!s.empty()) {
s.pop();
words.push_back("pop");
}
}
}
for (auto word : words) {
std::cout << word << std::endl;
}
}