解题报告 之 UVA120 Stacks of Flapjacks

来源:互联网 发布:kali linux 官方源 编辑:程序博客网 时间:2024/06/14 19:41

解题报告 之 UVA120 Stacks of Flapjacks

Description


Background

Stacks and Queues are often considered the bread and butter of data structures and find use in architecture, parsing, operating systems, and discrete event simulation. Stacks are also important in the theory of formal languages.

This problem involves both butter and sustenance in the form of pancakes rather than bread in addition to a finicky server who flips pancakes according to a unique, but complete set of rules.

The Problem

Given a stack of pancakes, you are to write a program that indicates how the stack can be sorted so that the largest pancake is on the bottom and the smallest pancake is on the top. The size of a pancake is given by the pancake's diameter. All pancakes in a stack have different diameters.

Sorting a stack is done by a sequence of pancake ``flips''. A flip consists of inserting a spatula between two pancakes in a stack and flipping (reversing) the pancakes on the spatula (reversing the sub-stack). A flip is specified by giving the position of the pancake on the bottom of the sub-stack to be flipped (relative to the whole stack). The pancake on the bottom of the whole stack has position 1 and the pancake on the top of a stack of n pancakes has position n.

A stack is specified by giving the diameter of each pancake in the stack in the order in which the pancakes appear.

For example, consider the three stacks of pancakes below (in which pancake 8 is the top-most pancake of the left stack):

         8           7           2         4           6           5         6           4           8         7           8           4         5           5           6         2           2           7
The stack on the left can be transformed to the stack in the middle via flip(3). The middle stack can be transformed into the right stack via the command flip(1).

The 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.

The 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

1 2 3 4 55 4 3 2 15 1 2 3 4

Sample Output

1 2 3 4 505 4 3 2 11 05 1 2 3 41 2 0

---------------------------------------------------------------------

今天这道题让我的计划从一天三道改成一天一道,卡成狗了。。尼玛。

题目大意:锅里有n张煎饼(n<=30)。每张煎饼有一个数字,代表它的大小。厨师每次可以选择一个数k,从锅底开始数,将第k张一直到顶的全部煎饼全部颠倒过来。(即选择的那张煎饼以及所以它上面的煎饼)。设计一种方法使得所有煎饼按照从小到大排序。输入时,各个煎饼按照从上到下的顺序给出。


首先需要注意这个题的输出需要将输入的序列再输出一次,不要忘了这个格式问题。

然后我们必须发现这个题是没有煎饼数输入的。所以我们必须先解决输入问题。本来采用了一种很奇葩的getchar的思路,但是后来发现RE了。所以借鉴了一种字符流的方法,我觉得这个是一个比较大的收获,可以解决那些没有输入数量而是以回车作为结束的所有情况。具体方法是用getline方法输入一行到字符串StringLine中,再用StringLine构造字符流iss,最后直接使用iss作为输入即可。


下面进入正题:Rank[i]表示煎饼堆上从上到下第i个煎饼是第几大的,(可以将数据压缩)。然后循环执行以下过程。先找到煎饼中最大的,然后看它的大小位置是否已经正确,如果正确就不管了。如果不正确,就再看它是否在顶部,如果是的话将它换到底部(即整个颠倒过来),输出当前 最下面的煎饼的位置;如果它不在顶部,则先将它换到顶部,输出当前最大煎饼的位置,再换到底部,输出当前最下面的煎饼的位置。注意此时第一张最大煎饼已经放到了最后,接下来我们只需要抛弃最后一张已经正确了的煎饼,对上面的煎饼重复执行以上操作,直到所有煎饼都放到了正确的位置。


下面上代码:

#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <sstream>#include <cstdio>using namespace std;int maxloc(int a[], int l, int r){int key = l;for ( int i = l; i <= r; i++){if (a[i] >= a[key])key = i;}return key;}struct pank{int loc;int size;bool operator <(const pank& rhs)const{return size < rhs.size;}};void SWAP(int a[], int l, int r){int i = l, j = r;while (i < j){int tem = a[i];a[i] = a[j];a[j] = tem;i++; j--;}}pank pan[40];int Rank[40];pank tem[40];int main(){for (string strLine; getline(cin,strLine);){cout << strLine << endl;istringstream iss(strLine);int n = 0;while (iss >> pan[n].size){pan[n].loc = n;tem[n] = pan[n];n++;}if (n == 0)break;sort(tem, tem + n);int ans = 0;for (int i = 0; i < n; i++)Rank[tem[i].loc] = i;for (int i = n-1; i >=0; i--){int key = maxloc(Rank, 0, i);if (key != i){if (key != 0){SWAP(Rank, 0, key);cout << n - key << " ";}SWAP(Rank, 0, i);cout << n- i << " ";}}cout << 0 << endl;}return 0;}

最后说一下,大大今天让我怒刷10道,我回他呵呵。


0 0
原创粉丝点击