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 ...
随机推荐
- kafka消费者启动报错
报错信息: WARN Fetching topic metadata with correlation id 14 for topics [Set(test)] from broker [Broker ...
- 输出映射resultMap
①:编写接口方法 /** * 根据id查询用户 * @param id * @return */ public User queryUserById3(Integer id); ②:编写映射文件 1: ...
- linux 下 ifcfg-ethx配置和解析
网络接口配置文件[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0# Intel Corporation 82545EM ...
- FE 命令随笔
FE_CMD ————— * >>>>>>>> Vue ________________________________________________ ...
- MongoDB 通过配置文件启动及注册服务
1.配置mongodb环境变量,配置完成之后就可以直接执行mong.mongod等常用命令,不用每次都到mongodb安装目录bin下去执行: 2.通过命令启动mongo服务 mongod --dbp ...
- 【代码笔记】Web-JavaScript-JavaScript正则表达式
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- TFS 2017 持续集成速记
VS2017许多激动人 心的功能,升级! TFS2017也升级,不支持SQL2012,升级!不过貌似开发版不能升级,好吧,开发版升2014企业版! 2017据说不支持XAML生成了,但后台菜单中还 ...
- LVS + nginx实现高性能精准负载均衡
- C# 利用PrintDocument定制打印单据
本文是利用PrintDocument定制打印单据的小例子,仅供学习分享使用,如果不足之处,还请指正. 涉及知识点: PrintDocument :从 Windows 窗体应用程序打印时,定义一种可重用 ...
- 小技巧:在线生成按钮Shape的网站
AndroidButton Make 右侧设置按钮的属性,可以即时看到效果,并即时生成对应的.xml 代码,非常高效(当然熟练的话 自己手写代码更快)