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 ...
随机推荐
- C#设计模式之十三模板方法模式(Template Method Pattern)【行为型】
一.引言 “结构型”的设计模式已经写完了,从今天我们开始讲“行为型”设计模式.现在我们开始讲[行为型]设计模式的第一个模式,该模式是[模板方法],英文名称是:Template Method Patte ...
- Linux服务器配置
配置ssh: 1. 查看22端口是否监听 netstat -antu | grep :22 2. 安装ssh服务 sudo apt-get install ssh 3. 再次查看22端口 安装apac ...
- 用Python实现Zabbix-API 监控
做运维的朋友应该知道,公司IDC机房经常有上架.下架.报修和报废的服务器.如果服务器数量很多的时候很容易造成监控遗漏. 大的互联网公司把监控系统和CMDB(资产管理系统|配置管理数据库系统 ...
- hive drop 报错
执行 drop database xxxx cascade; 删除数据库的时候报错. 报错信息:Execution Error, return code 1 from org.apache.hadoo ...
- MVC模式-----struts2框架
MVC模式-----struts2框架 一.struts MVC模式 1.概述 MVC是模型(model)---视图model(view)---控制器(control)的缩写,是一种用于将逻辑.数据和 ...
- 小tips:JS数值之间的转换,JS中最大的Number是多少?,JS == 与 === 的区别
JS数值之间的转换 Number(), parseInt(),parseFloat() Number()函数的转换规则如下: 1.如果boolean值,true和false将分别被转换为1和02.如果 ...
- Dynamics 365 Online-Unified User Interface
为了加强界面的一致性,提高用户体验,Dynamics 365 Online V9发布了新的Interface:Unified User Interface. 区别于旧的Regular UI,UUI自适 ...
- mysql随笔系列-1
MySQL数据库管理 本人实验所用的MySQL数据库版本:5.5.56-MariaDB MariaDB Server 操作系统:centos7.5 1.创建数据库 MariaDB [(none)]& ...
- nginx中有关 root 和 alias的主要区别
举个例子给伙伴们区别就明显看出来了,例子如下: location /img/ { alias /var/www/image/; }注意:如果按照上述配置的话,则访问/img/目录里面的文件时,ning ...
- 活字格对接RF手持机与ERP系统,帮助RFID快速盘点方案落地
这个玩意有点意思,要不要搞来玩玩? ----------- 射频技术(RF)是Radio Frequency的缩写,较常见的应用有无线射频识别(Radio Frequency Identificati ...