-
Notifications
You must be signed in to change notification settings - Fork 0
/
P15500.cpp
59 lines (49 loc) · 1.27 KB
/
P15500.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
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N;
cin >> N;
vector<int> L;
vector<int> M;
vector<pii> result;
int MAX = 0;
int T = 0;
for (int i = 0; i < N; i++) {
int v;
cin >> v;
MAX = max(MAX, v);
L.push_back(v);
}
T = MAX;
while (!L.empty() || !M.empty()) {
if ((!L.empty()) && L[L.size() - 1] == T) {
L.pop_back();
T--;
result.push_back({1, 3});
} else if ((!L.empty()) && L[L.size() - 1] != T) {
int top = L[L.size() - 1];
L.pop_back();
M.push_back(top);
result.push_back({1, 2});
} else if ((!M.empty()) && M[M.size() - 1] == T) {
M.pop_back();
T--;
result.push_back({2, 3});
} else if ((!M.empty()) && M[M.size() - 1] != T) {
int top = M[M.size() - 1];
M.pop_back();
L.push_back(top);
result.push_back({2, 1});
}
}
cout << result.size() << "\n";
for (const auto & i : result) {
cout << i.first << " " << i.second << "\n";
}
return 0;
}