UVA120-Stacks of Flapjacks(思维)
Accept: 9232 Submit: 38455
Time Limit: 10000 mSec
Problem Description
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.
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
Sample Output
1 2 3 4 5
0
5 4 3 2 1
1 0
5 1 2 3 4
1 2 0
题解:虽说是道脑洞题,但是稍加思考应该问题不大,第一想法先把大的搞定,因为大的一定就不用动了,相当于缩小了问题规模。有了这个想法肯定就去想怎么把最大的放到最下面,稍加尝试就出来了。
#include <bits/stdc++.h> using namespace std; vector<int> num, sort_num;
vector<int> ans; bool cmp(const int &a, const int &b) {
return a > b;
} void init() {
ans.clear();
num.clear();
sort_num.clear();
} int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
string str;
while (getline(cin, str)) {
init();
cout << str << endl;
stringstream ss(str); int cnt, xx;
while (ss >> xx) num.push_back(xx), sort_num.push_back(xx);
sort(sort_num.begin(), sort_num.end(), greater<int>());
cnt = num.size(); for (int i = ; i < cnt; i++) {
int x = sort_num[i];
vector<int>::iterator iter = num.begin();
for (; iter != num.end(); iter++) {
if (*iter == x) break;
}
int pos = iter - num.begin();
//printf("pos : %d\n", pos);
if (pos == cnt - i - ) continue;
if (pos != ) {
iter++;
reverse(num.begin(),iter);
ans.push_back(cnt - pos);
//for (int i = 0; i < cnt; i++) printf("%d ", num[i]);
//printf("\n");
}
vector<int>::iterator iter2 = num.begin();
int k = cnt - i;
while (k--) iter2++;
reverse(num.begin(), iter2);
ans.push_back(i + );
//for (int i = 0; i < cnt; i++) printf("%d ", num[i]);
//printf("\n");
}
for (int i = ; i < ans.size(); i++) {
printf("%d ", ans[i]);
}
printf("0\n");
}
return ;
}
UVA120-Stacks of Flapjacks(思维)的更多相关文章
- UVa120 - Stacks of Flapjacks
Time limit: 3.000 seconds限时:3.000秒 Background背景 Stacks and Queues are often considered the bread and ...
- Uva120 Stacks of Flapjacks 翻煎饼
水水题.给出煎饼数列, 一次只能让第一个到第i个数列全部反转,要求把数列排序为升序. 算法点破后不值几钱... 只要想办法把最大的煎饼放到最后一个,然后就变成前面那些煎饼的数列的子题目了.递归或循环即 ...
- uva120 Stacks of Flapjacks (构造法)
这个题没什么算法,就是想出怎么把答案构造出来就行. 思路:越大的越放在底端,那么每次就找出还没搞定的最大的,把它移到当前还没定好的那些位置的最底端,定好的就不用管了. 这道题要处理好输入,每次输入的一 ...
- 【思维】Stacks of Flapjacks
[UVa120] Stacks of Flapjacks 算法入门经典第8章8-1 (P236) 题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列. 试题分析:从插入排序即可找到 ...
- uva 120 stacks of flapjacks ——yhx
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data ...
- UVaOJ 120 - Stacks of Flapjacks
120 - Stacks of Flapjacks 题目看了半天......英语啊!!! 好久没做题...循环输入数字都搞了半天...罪过啊!!! 还是C方便一点...其实C++应该更方便的...C+ ...
- Uva 120 - Stacks of Flapjacks(构造法)
UVA - 120 Stacks of Flapjacks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &a ...
- uva Stacks of Flapjacks
Stacks of Flapjacks 题目链接:Click Here~ 题目描写叙述: ...
- Stacks of Flapjacks(栈)
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data ...
- Stacks of Flapjacks
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data s ...
随机推荐
- 将Hexo博客部署到云主机
摘要: 在云主机上搭建一个git裸仓库,然后使用nginx作为网页服务器,就可以轻松将Hexo博客通过git部署到云主机上. 这是一个忧伤的故事 我的博客KiwenLau之前部署在Coding Pag ...
- gulp简介
简介 gulp是基于Nodejs的自动任务运行器, 她能自动化地完成 javascript/coffee/sass/less/html/image/css 等文件的的测试.检查.合并.压缩.格式化.浏 ...
- BZOJ4559: [JLoi2016]成绩比较(dp 拉格朗日插值)
题意 题目链接 Sol 想不到想不到.. 首先在不考虑每个人的真是成绩的情况下,设\(f[i][j]\)表示考虑了前\(i\)个人,有\(j\)个人被碾压的方案数 转移方程:\[f[i][j] = \ ...
- angularJS中控制器和作用范围
$scope是$rootScope的子作用域控制对象,$rootScope的id为1,其他的为2,3,4... 不同的控制器之间,所对应的作用域控制对象$scope,之间是相互隔离的,如果要共享数据, ...
- 也说Socket
网上有大量socket相关文章,茫茫多,大多交代不清,最近自我整理了一下socket相关知识,附加了大量代码注释,先看效果. 上代码,客户端: Socket socket1 = null;//一个全局 ...
- vue2 设置网页title的问题
好东东,没个标题看着多难受 看到1文章 http://blog.csdn.net/qishuixian/article/details/72912368 推荐使用 vue-wechat-title插 ...
- Hibernate概念初探
概述 Hibernate是一个开源代码的对象关系映射(ORM)框架,是基于Java的持久化中间件,它对JDBC进行轻量级的对象封装. 它不仅提供了从Java类到数据表之间的映射,也提供了查询和事务机制 ...
- 19.Odoo产品分析 (二) – 商业板块(11) – 在线活动(1)
查看Odoo产品分析系列--目录 点击安装"在线活动". 1. 主页 在线活动绑定在电子商务中,在网站中可以看到在线活动的菜单: 在这里可以按时间看到每一个活动. 2. 新建活动 ...
- 16.Odoo产品分析 (二) – 商业板块(9) – 网站生成器(1)
查看Odoo产品分析系列--目录 安装"电子商务"模块时,该模块会自动安装,但网站生成器是电子商务的前提,因此,先分析该模块,在下面就是对电子商务模块的分析. 1. 编辑网站 安装 ...
- 「Android」adb调试源码(针对dumpsys SurfceFlinger、trace.txt获取)
首先对ADB作简单的阐述,接下来对adb shell dumpsys SurfaceFlinger服务的dump信息的查看.以及ANR问题如何获取trace文件并简单分析. -×*********** ...