UVA 120 Stacks of Flapjacks(煎饼)

题意:一叠煎饼,每个煎饼都有一个数字,每次可以选择一个数k,把从锅底开始数第k张以及其上面的煎饼全部翻过来,最终使煎饼有序排列(锅顶最小,锅底最大)。

分析:依次从锅底向上,优先排数字最大的煎饼。每次找未排好序列中数字最大的煎饼,并把他翻到锅顶,再将整个未排好序的序列翻转,这样该数字就到了当所有煎饼有序排列时其所在的位置。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 10000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
string s;
vector<int> a;
vector<int> b;
int len;
void Reverse(int st, int et){//翻转
    int len = (et - st) / 2;
    for(int i = 0; i <= len; ++i){
        swap(a[i], a[et - i]);
    }
}
bool judge(){
    for(int i = 0; i < len; ++i){
        if(a[i] != b[i]) return false;
    }
    return true;
}
int main(){
    while(getline(cin, s)){
        a.clear();
        b.clear();
        stringstream ss(s);
        int ma = 0;//最大值
        int x;
        while(ss >> x){
            a.push_back(x);
            b.push_back(x);
            ma = Max(ma, x);
        }
        sort(b.begin(), b.end());
        len = a.size();
        for(int i = 0; i < len; ++i){
            if(i) printf(" ");
            printf("%d", a[i]);
        }
        printf("\n");
        int t = len;
        while(1){
            if(judge()) break;//已排好序
            --t;
            for(int i = 0; i < len; ++i){
                if(a[i] == b[t]){
                    if(i != t){//若当前要排的数已在其排好序的位置则不做翻转
                        if(i != 0){//若不在顶部
                            Reverse(0, i);
                            printf("%d ", len - i);
                        }
                        Reverse(0, t);
                        printf("%d ", len - t);
                    }
                    break;
                }
            }
        }
        printf("0\n");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6326338.html