Accept: 9232 Submit: 38455
Time Limit: 10000 mSec
Problem Description
Input
The input consists of a sequence of stacks of pancakes. Each stack will consist of between 1 and 30 pancakes and each pancake will have an integer diameter between 1 and 100. The input is terminated by end-of-file. Each stack is given as a single line of input with the top pancake on a stack appearing first on a line, the bottom pancake appearing last, and all pancakes separated by a space.
Output
For each stack of pancakes, the output should echo the original stack on one line, followed by some sequence of flips that results in the stack of pancakes being sorted so that the largest diameter pancake is on the bottom and the smallest on top. For each stack the sequence of flips should be terminated by a ‘0’ (indicating no more flips necessary). Once a stack is sorted, no more flips should be made.
Sample Input
Sample Output
1 2 3 4 5
0
5 4 3 2 1
1 0
5 1 2 3 4
1 2 0
题解:虽说是道脑洞题,但是稍加思考应该问题不大,第一想法先把大的搞定,因为大的一定就不用动了,相当于缩小了问题规模。有了这个想法肯定就去想怎么把最大的放到最下面,稍加尝试就出来了。
#include <bits/stdc++.h> using namespace std; vector<int> num, sort_num;
vector<int> ans; bool cmp(const int &a, const int &b) {
return a > b;
} void init() {
ans.clear();
num.clear();
sort_num.clear();
} int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
string str;
while (getline(cin, str)) {
init();
cout << str << endl;
stringstream ss(str); int cnt, xx;
while (ss >> xx) num.push_back(xx), sort_num.push_back(xx);
sort(sort_num.begin(), sort_num.end(), greater<int>());
cnt = num.size(); for (int i = ; i < cnt; i++) {
int x = sort_num[i];
vector<int>::iterator iter = num.begin();
for (; iter != num.end(); iter++) {
if (*iter == x) break;
}
int pos = iter - num.begin();
//printf("pos : %d\n", pos);
if (pos == cnt - i - ) continue;
if (pos != ) {
iter++;
reverse(num.begin(),iter);
ans.push_back(cnt - pos);
//for (int i = 0; i < cnt; i++) printf("%d ", num[i]);
//printf("\n");
}
vector<int>::iterator iter2 = num.begin();
int k = cnt - i;
while (k--) iter2++;
reverse(num.begin(), iter2);
ans.push_back(i + );
//for (int i = 0; i < cnt; i++) printf("%d ", num[i]);
//printf("\n");
}
for (int i = ; i < ans.size(); i++) {
printf("%d ", ans[i]);
}
printf("0\n");
}
return ;
}