题意:翻煎饼,翻成从小到大排序。
分析:每次都找最大的移动,最大的前面煎饼就不用考虑了。
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
#include<vector>
using namespace std;
int a[100 + 5];
vector<int>q, p, m;
int len = 0;
int ans = 0;
bool check() {
for (int i = 0; i < len-1; i++) {
if (p[i + 1] < p[i]) {
return false;
}
}
return true;
}
void solve() {
int maxt = a[0];
int indext=0;
while (!check()) {
maxt = p[0];
indext = 0;
for (int i = 0; i < len; i++) {
if (maxt < p[i]) {
maxt = p[i];
indext = i;
}
}
q.clear();
if (indext == len - 1) { len--; continue; }
if (indext)cout <<ans- indext-1 << " ";
cout << ans-len<<" ";
for (int i = indext; i >= 0; i--) {
q.push_back(p[i]);
}
for (int i = indext + 1; i < len; i++) {
q.push_back(p[i]);
}
p.clear();
for (int i = len - 1; i >= 0; i--) {
p.push_back(q[i]);
}
len--;
}
}
int main() {
string s;
string sz;
while (getline(cin, s)) {
len = 0;
p.clear(); q.clear();
stringstream ss(s);
while (ss >> sz) {
a[len++]=atoi(sz.c_str());
p.push_back(a[len - 1]);
}
for (int i = 0; i < len - 1; i++)cout << a[i]<<" ";
cout << a[len - 1]<<endl;
ans = len+1;
solve();
cout << "0"<<endl;
}
return 0;
}