-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path120.cpp
78 lines (66 loc) · 1.7 KB
/
120.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// iagorrr ;)
#include <bits/stdc++.h>
using namespace std;
vector<int> split(string s){
istringstream i(s);
int x;
vector<int> a;
while(i >> x){
a.push_back(x);
}
return a;
}
bool check(vector<int> a){
for(int i = 0; i < a.size()-1; ++i)
if(a[i] != i+1)
return false;
return true;
}
void solve(string s){
// cout << "=====================================\n";
// to put an element at top flip his position and then the entire stack.
vector<int> o = split(s);
vector<int> ans;
// put the largest value until now at the bottom.
vector<int> m = o;
int n = o.size();
for(int i = n-1; i >= 0; --i){
// for(auto x : m)
// cout << x << ' ';
// cout << '\n';
int mpos = i;
for(int j = i-1; j >= 0; --j){
if(m[j] > m[mpos]){
mpos = j;
}
}
// the largest is in the correct position already.
if(mpos == i) continue;
// put the largest at top, so we can move it to whatever position.
if(mpos != 0){
// cout << "p1 " << mpos << '\n';
ans.push_back(n-mpos);
reverse(m.begin(), m.begin()+mpos+1 );
}
// with the largest one at the top put it at the respective bottom.
// cout << "p2 " << i << '\n';
ans.push_back(n-i);
reverse(m.begin(), m.begin()+i+1);
}
// output the original.
for(auto x : o)
cout << x << ' ';
cout << '\n';
// output the flips.
for(auto x : ans)
cout << x << ' ';
cout << "0\n";
}
int main(){
string s;
while(getline(cin, s)){
solve(s);
}
return 0;
}
// AC.