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 ...
随机推荐
- java连接MySQL数据库的方式
Java连接数据库的几种方法 *说明 1.以MySQL数据库为例 2.分为四个步骤: 建立数据库连接, 向数据库中提交sql 处理数据库返回的结果 关闭数据库连接 一:JDBC 1.建立数据库连接 只 ...
- Spring的xml解析原理分析【转载】
一:前言 二:spring的配置文件 三:依赖的第三方库.使用技术.代码布局 四:Document实现 五:获取Element的实现 六:解析Element元素 七:Bean创造器 八:Ioc容器的创 ...
- 事件处理程序(HTML)
HTML事件处理程序 通过设置HTML标签特性来绑定事件处理程序. 处理方式(如图): 1:会创建一个封装元素属性值的函数 2:会在这个函数中创建一个event事件对象 <form> &l ...
- angular select 默认值
<select ng-model="selected" ng-options="x.id as x.name for x in users">< ...
- angular ng-click防止重复提交
方法一:点击后,让button的状态变为disable js指令: .directive('clickAndDisable', function() { return { scope: { click ...
- angular 拼接html 事件无效
主要是要引用$compile方法
- 洛谷P4065 [JXOI2017]颜色(线段树)
题意 题目链接 Sol 线段树板子题都做不出来,真是越来越菜了.. 根据题目描述,一个合法区间等价于在区间内的颜色没有在区间外出现过. 所以我们可以对于每个右端点,统计最长的左端点在哪里,刚开始以为这 ...
- 21.Odoo产品分析 (三) – 人力资源板块(2) – 工时表(1)
查看Odoo产品分析系列--目录 工时表是一个用来管理员工工作时间和出勤的模块.当需要计算员工的工作时间,并将这些时间对应到项目或者顾客上时,"工时表"就会起到一个非常好的作用. ...
- Linux Linux下最大文件描述符设置
Linux下最大文件描述符设置 by:授客 QQ:1033553122 1. 系统可打开最大文件描述符设置 查看系统可打开最大文件描述符 # cat /proc/sys/fs/file-max 6 ...
- Android为TV端助力 Intent匹配action,category和data原则
1.当你在androidmanifest里面定义了一个或多个action时 你使用隐式意图其他activity或者service时,规定你隐式里面的action必须匹配XML中定义的action,可以 ...