HDU 1988 ZOJ 2991 Flipping Burned Pancakes(数学啊+模拟)

2024-01-14 05:59

本文主要是介绍HDU 1988 ZOJ 2991 Flipping Burned Pancakes(数学啊+模拟),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:

HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1988

ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1990


Problem Description
The cook at the Frobbozz Magic Pancake House sometimes falls asleep on the job while cooking pancakes. As a result, one side of a stack of pancakes is often burned. Clearly, it is bad business to serve visibly burned pancakes to the patrons. Before serving, the waitress will arrange the stacks of pancakes so that the burned sides are facing down. You must write a program to aid the waitress in stacking the pancakes correctly.
We start with a stack of N pancakes of distinct sizes, each of which is burned on one side. The problem is to convert the stack to one in which the pancakes are in size order with the smallest on the top and the largest on the bottom and burned side down for each pancake. To do this, we are
allowed to flip the top k pancakes over as a unit (so the k-th pancake is now on top and the pancake previously on top is now in the k-th position and the burned side goes from top to bottom and vice versa).
For example (+ indicates burned bottom, - a burned top):



You must write a program which finds a sequence of at most (3n – 2) flips, which converts a given stack of pancakes to a sorted stack with burned sides down.
Input
The first line of the input contains a single decimal integer, N, the number of problem instances to follow. Each of the following N lines gives a separate dataset as a sequence of numbers separated by spaces. The first number on each line gives the number, M, of pancakes in the data set. The remainder of the data set is the numbers 1 through M in some order, each with a plus or minus sign, giving the initial pancake stack. The numbers indicate the relative sizes of the pancakes and the signs indicate whether the burned side is up (-) or down (+). M will be, at most, 30.
Output
For each dataset, you should generate one line of output with the following values: The dataset number as a decimal integer (start counting at one), a space, the number of flips (K, where K >= 0) required to sort the pancakes and a sequence of K numbers, each of which gives the number of pancakes to flip on the corresponding sorting step. There may be several correct solutions for some datasets. For instance 3 2 3 is also a solution to the first problem below.
Sample Input
  
3 3 +1 -3 -2 4 -3 +1 -2 -4 5 +1 +2 +3 +4 -5
Sample Output
  
1 6 2 1 3 1 2 1 2 6 4 1 4 3 1 2 3 3 5 1 5


题意:

一共有n块煎饼,面积分别是从1到n,正面朝上为‘+’,反面朝上为‘-’。

每次可以选择上面连续的若干块一起翻转过来。

求翻转的步骤,可以使得最后 正面朝上,面积从上到下是从小到大。

代码如下:

#include <cstdio>
#include <cmath>
int a[47];
void reserve (int pos)
{int tt;for(int i = 1, j = pos; i < j; i++,j--){tt = a[i],a[i] = a[j],a[j] = tt;}for(int i = 1; i <= pos; i++){a[i] = -a[i];}
}
int main()
{int t;int n;int cas = 0;int b[147];scanf("%d",&t);while(t--){int k = 0;scanf("%d",&n);for(int i = 1; i <= n; i++){scanf("%d",&a[i]);}int p = 0;for(int i = n; i >= 1; i--)//先放最大的{if(a[i] == i)continue;//已经放好了,既然等于i那么一定是向上的for(int j = 1; j <= n; j++){if(fabs(a[j]) == i)//找到应该放下面的那块煎饼的位置{p = j;}}if(p != 1)//把当前要放到下面的先放到最上面,如果本来就在最上面这步就不需要了{reserve(p);b[k++] = p;}if(a[1] > 0)//如果最上面是向上,则翻为向下,因为下面还要再翻一次{a[1] = -a[1];b[k++] = 1;}if(i != 1)//把最上面的放到下面{b[k++] = i;reserve(i);}else{b[k++] = 1;a[1] = -a[1];}}printf("%d %d",++cas,k);for(int i = 0; i < k; i++){printf(" %d",b[i]);}printf("\n");}return 0;
}


这篇关于HDU 1988 ZOJ 2991 Flipping Burned Pancakes(数学啊+模拟)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/604152

相关文章

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

usaco 1.2 Transformations(模拟)

我的做法就是一个一个情况枚举出来 注意计算公式: ( 变换后的矩阵记为C) 顺时针旋转90°:C[i] [j]=A[n-j-1] [i] (旋转180°和270° 可以多转几个九十度来推) 对称:C[i] [n-j-1]=A[i] [j] 代码有点长 。。。 /*ID: who jayLANG: C++TASK: transform*/#include<

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

uva 10014 Simple calculations(数学推导)

直接按照题意来推导最后的结果就行了。 开始的时候只做到了第一个推导,第二次没有继续下去。 代码: #include<stdio.h>int main(){int T, n, i;double a, aa, sum, temp, ans;scanf("%d", &T);while(T--){scanf("%d", &n);scanf("%lf", &first);scanf

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

uva 10025 The ? 1 ? 2 ? ... ? n = k problem(数学)

题意是    ?  1  ?  2  ?  ...  ?  n = k 式子中给k,? 处可以填 + 也可以填 - ,问最小满足条件的n。 e.g k = 12  - 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12 with n = 7。 先给证明,令 S(n) = 1 + 2 + 3 + 4 + 5 + .... + n 暴搜n,搜出当 S(n) >=