#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <ctime>
#include <functional>
#include <string.h>
#include <stdio.h>
#include <numeric>
#include <float.h>
using namespace std;
void change(vector<int> &v, int pos) {
vector<int> tv;
int realPos = v.size() - pos;
for (int i = 0; i <= realPos; i++) {
tv.push_back(v[i]);
}
for (int i = realPos; i >= 0; i--) {
v[i] = tv[realPos - i];
}
}
void solution(vector<int> &v, int pos, vector<int> &result) {
if (pos == v.size()) return;
int maxPos = 0;
for (int i = 1; i < (v.size() - (pos - 1)); i++) {
if (v[maxPos] < v[i]) {
maxPos = i;
}
}
int realPos = v.size() - maxPos;
if (realPos != pos) {
// move to 0
if (maxPos != 0) {
result.push_back(realPos);
change(v, realPos);
}
result.push_back(pos);
change(v, pos);
}
solution(v, pos + 1, result);
}
int main() {
string s;
while (!cin.eof() && getline(cin, s)) {
stringstream ss(s);
vector<int> v;
int i;
while (ss >> i) {
v.push_back(i);
}
cout << s << endl;
vector<int> result;
solution(v, 1, result);
bool blank = false;
for (int i = 0; i < result.size(); i++) {
if (blank) cout << " ";
blank = true;
cout << result[i];
}
if (blank) cout << " ";
cout << 0 << endl;
}
return 0;
}
programming-challenges Stacks of Flapjacks (110402) 题解
最新推荐文章于 2018-07-31 14:34:58 发布