uva 120 C - Stacks of Flapjacks

题意:给一个n长度的子序列,每次可以把从下往上数,第几个及上面的都反转过来,最后序列变为上升子序列

分析:可以每次把当前最大的放到下面,这样以后无论如何翻动都不会干扰到他,直到得到答案

题目没有要求最优解,这样肯定能得到结果,借助函数reverse反转数组效果不错,没难度,见代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=35;
 4
 5 struct point{
 6    int id,x;
 7 }p[maxn];
 8
 9 bool cmp(point a,point b){
10    return a.x<b.x;
11 }
12
13 int num[maxn];
14
15 int main(){
16    int n,d;
17    string line;
18    while(getline(cin,line)){
19       stringstream ccin(line);
20       n=0;
21       while(ccin>>d){
22          p[n].x=d;
23          p[n].id=n+1;
24          n++;
25       }
26       for(int i=0;i<n-1;i++)
27          cout<<p[i].x<<" ";
28       cout<<p[n-1].x<<endl;
29       sort(p,p+n,cmp);
30       for(int i=0;i<n;i++)
31          num[p[i].id-1]=i+1;
32       int ans=n;
33       while(n>1){
34          int i;
35          for(i=n-1;i>=0;i--)
36             if(num[i]==n)
37                break;
38          if(i==0){
39             reverse(num,num+n);
40             n--;
41             cout<<ans-n<<" ";
42          }
43          else if(i==n-1)
44             n--;
45          else{
46             cout<<ans-i<<" ";
47             reverse(num,num+i+1);
48             reverse(num,num+n);
49             n--;
50             cout<<ans-n<<" ";
51          }
52
53       }
54       cout<<0<<endl;
55    }
56    return 0;
57 }

时间: 2024-08-17 21:21:34

uva 120 C - Stacks of Flapjacks的相关文章

UVa 120 (构造) Stacks of Flapjacks

这题求解的过程和选择排序非常相似. 反转的过程中分为无序(在前面)和有序(在后面)两个部分,一开始视为全部为无序. 在无序部分中找到最大的元素,先把它翻到最前面,然后再反转到无序部分的最后面.这样该元素就成为有序的部分. 而且在算法执行的过程中不会影响到已经构造好的有序部分. 1 #include <iostream> 2 #include <string> 3 #include <sstream> 4 #include <algorithm> 5 #inc

[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 UVA 120

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

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(构造法)

题目地址:UVa 120 水题.从最大的开始移,每次都把大的先翻到最上面,再翻到下面. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include

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

uva Stacks of Flapjacks

                                                 Stacks of Flapjacks  题目链接:Click Here~ 题目描写叙述:     给你n个数.要你得到的最后结果是从下到大排序.可是给出的序列不一定是有序你.要通过你的调整.问:要经过哪几个位置上的数的调整? 算法分析:     一開始,我的思路是直接模拟一边消除逆序数就能够了,由于我看数据非常小,仅仅有30.可是提交之后却TEL了. 后来上网查了一下.看到有一个人的思路还是非

【思维】Stacks of Flapjacks

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

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