uva 120——Stacks of Flapjacks
来源:互联网 发布:维普数据库使用方法 编辑:程序博客网 时间:2024/05/29 10:33
题意:一个非常有趣的问题,就是给一摞煎饼,然后从下面拿起来一张,然后把该张上面的所有馅饼反转,求最后使得馅饼从小到大的最小的步数。
思路:贪心。每次都把没排好序的最大数反转到最上面,然后在一次反转到最下面,也就是最多需要2n步就能够把所有的馅饼排好。
Code:
#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <sstream>#include <cstring>#include <cmath>#include <map>#include <vector>#include <queue>#include <set>using namespace std;typedef long long ll;typedef unsigned long long ull;const int N=1000000;const int M=105;const int mod=10000007;const int INF=0x3f3f3f3f;const ll LINF=0x3f3f3f3f3f3f3f3f;const double eps=1e-9;const double pai=acos(-1.0);#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define cls(x,c) memset(x,c,sizeof(x))#define fr(i,n) for (int i=0;i<n;i++)int a[35],len;int tran(int j){ int t=j; for (int i=0;i<j;i++) if (a[i]>a[t]) t=i; return t;}int main(){ string s; int i,j; while (getline(cin,s)) { if (s=="") break; cout<<s<<endl; stringstream ss; ss<<s; len=0; while (ss>>a[len]) len++; //fr (i,len) cout<<"bug2"<<a[i]<<endl; for (int i=len-1;i>=0;i--) { j=tran(i); //cout<<"bug "<<j<<endl; if (j==i) continue; if (j!=0) { printf("%d ",len-j); reverse(a,a+j+1); } reverse(a,a+i+1); printf("%d ",len-i); } puts("0"); }}
0 0
- uva 120——Stacks of Flapjacks
- uva 120 stacks of flapjacks ——yhx
- uva 120 stacks of flapjacks ——yhx
- UVA 120 Stacks of Flapjacks ——思路题
- UVA 120 Stacks of Flapjacks
- UVa 120 - Stacks of Flapjacks
- uva 120 Stacks of Flapjacks
- UVA 120 - Stacks of Flapjacks
- uva 120 - Stacks of Flapjacks
- UVa 120 - Stacks of Flapjacks
- UVA 120 - Stacks of Flapjacks
- UVa 120 - Stacks of Flapjacks
- UVA 120Stacks of Flapjacks
- UVA 120 - Stacks of Flapjacks
- uva 120 Stacks of Flapjacks
- UVA 120 Stacks of Flapjacks
- uva 120 Stacks of Flapjacks
- uva - 120 - Stacks of Flapjacks
- 第十周 项目3-利用二叉树遍历思想解决问题(1)
- CentOS中由一般用户切换为root用户
- 歌手大赛计分函数版
- 第九周项目4-广义表算法库及应用
- Android音乐播放器(2)从SD卡中读取音乐
- uva 120——Stacks of Flapjacks
- python set
- 2015/11/06 iOS学习笔记
- Spring框架简介---模块
- HttpServletRequest类既有getAttribute()方法,也有getParameter()方法,有setAttribute()方法,而没有setParameter()方法
- 标题:用kill函数传递信号 int kill(pid_t pid,int sig)
- 第六周项目1-建立顺序栈算法库
- 第九周项目2 对称矩阵压缩存储的实现与应用
- 编程免费书Python,C/C++