Uva120 Stacks of Flapjacks

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 int a[31],n;
 6
 7 void rev(int ct) {
 8     printf("%d ", n-ct);
 9     for (int i=0;i<=ct/2;++i)
10         swap(a[i],a[ct-i]);
11 }
12
13 int scan()
14 {
15     char ch=0;
16     memset(a,0,sizeof(a));
17     if(scanf("%d",&a[0])==EOF)
18         return 0;
19     int i=1;
20     while((ch=getchar())&&ch!=‘\n‘)
21         scanf("%d",&a[i++]);
22     return i;
23 }
24 int main()
25 {
26     while(n=scan())
27     {
28         printf("%d",a[0]);
29         for(int i=1;i<n;++i)
30             printf(" %d",a[i]);
31         printf("\n");
32         int cnt=n;
33         while(cnt)
34         {
35             int maxn=0,flag;
36             for (int i=0;i<cnt;++i)
37             {
38                 if (a[i]>maxn)
39                 {
40                     maxn=a[i];
41                     flag=i;
42                 }
43             }
44             if(flag!=cnt-1)
45             {
46                 if(flag!=0)
47                     rev(flag);
48                 rev(cnt-1);
49             }
50             cnt--;
51         }
52         printf("0\n");
53     }
54     return 0;
55 }

题意:给出一些数列, 一次只能让第一个到第i个数列全部反转,要求把数列排序为升序。

时间: 2024-08-01 03:52:29

Uva120 Stacks of Flapjacks的相关文章

UVa120 - Stacks of Flapjacks (STL)

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. 栈和队列常常被

uva120 Stacks of Flapjacks (构造法)

这个题没什么算法,就是想出怎么把答案构造出来就行. 思路:越大的越放在底端,那么每次就找出还没搞定的最大的,把它移到当前还没定好的那些位置的最底端,定好的就不用管了. 这道题要处理好输入,每次输入的一行中是带空格的,以换行符结束一组数据的输入,那么用getline()函数.再用stringstream(这个可以自动转格式),非常方便的就处理了输入的问题.另外注意max_element和min_element都是左闭右开的. #include<iostream> #include<cstd

UVa120 Stacks of Flapjacks (构造法)

链接:http://vjudge.net/problem/18284 分析:摊煎饼问题.以从大到小的顺序依次把每个数排到正确的位置,比如当处理第i大的煎饼时,是不会影响到第1,2,3,...,i-1大的煎饼的(它们已经正确的翻到了煎饼堆底部的i-1个位置上),翻煎饼的方法是先翻到最上面,然后翻到正确的位置,翻好了以后煎饼堆底部i个位置上的煎饼都已经正确放好就不用管了,接下来就是找第i+1大的煎饼放到下数上第i+1个位置上,不过对于当前最大煎饼的位置要分类讨论,如果已经在其正确位置上就什么也不用做

【思维】Stacks of Flapjacks

[UVa120] Stacks of Flapjacks 算法入门经典第8章8-1 (P236) 题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列. 试题分析:从插入排序即可找到思路.每次我们优先地将没有到自己位置上的.最大的数挪到自己的位置上. 为什么可以这样做呢?难道不会改变已经排好序的么. 不会,因为我们从大往小处理,翻转的是前面的一段,而排好序的是后面一段,因此肯定不会打乱后面的. 对于每一个数,设其下标为pos,已经排好序的有x个,那么我们先将pos其变为1,即翻

[2016-03-03][UVA][120][Stacks of Flapjacks]

[2016-03-03][UVA][120][Stacks of Flapjacks] UVA - 120 Stacks of Flapjacks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Background Stacks and Queues are often considered the bread and butter of data s

Stacks of Flapjacks

Stacks of Flapjacks 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 l

uva 120 stacks of flapjacks ——yhx

 Stacks of Flapjacks  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

UVA 120 Stacks of Flapjacks(翻煎饼,啦啦啦啦~)

Problem A : Stacks of Flapjacks 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

Stacks of Flapjacks UVA 120

说明:这道题初读有点绕....开始我以为只是将一个序列的最大值放到最右边,把最小值放到最左边...所以就怎么也通不过啦.后来终于理解题意了,其实就是要你排个序,从左到右为从大到小.但是改变序列顺序的规则有些特殊,即只能将从最左边开始到某一位置构成的连续子序列进行对称翻转.如有序列:  2,4,9,5,7,6.我选择的位置是5所在的位置,翻转后序列就变为5,9,4,2,7,6.然后用类似于冒泡排序的方法.通过最多两次翻转可以得到最大值,然后放到右边.不断重复上述操作,即可得到一个有序序列. 题目: