Stacks
of Flapjacks
 



题目链接:Click Here~

题目描写叙述:

    给你n个数。要你得到的最后结果是从下到大排序。可是给出的序列不一定是有序你。要通过你的调整。问:要经过哪几个位置上的数的调整?

算法分析:

    一開始,我的思路是直接模拟一边消除逆序数就能够了,由于我看数据非常小,仅仅有30.可是提交之后却TEL了。

后来上网查了一下。看到有一个人的思路还是非常好的。就是说运用到了动态规划的思想。

确实非常巧妙啊!

    思路:在查询到当前第i个数的时候,你必须保证其后面的数是有序的,且都比i大。仅仅要保证了这一个条件之后,我们能够从快排的思想得知这是正确的。

但是怎样能够实现呢?事实上也非常easy。比方,但前你查询到了第i个了,但是第i个不满足条件。并且这是后又有两种情况。

(第i个数本应该的数在哪里?)1、可能就在最前面,那个此时你能够直接Flip操作就能够了。

2、不再最前面,此时你能够先找到最大数的位置,然后先将最大数Flip操作实现其到最前面,然后再实现操作1就OK了。

例:

  2 4 1 3 5

 此时我们查询到了3这个位置,你会发现3这个位置本来应该是4的可是如今不是,所以你要进行Flip操作。可是你有发现4不再最前面,即使你实现了Flip操作也得不到正确结果。

所以,你要先把4Flip到最前面,即2的位置。

4 2 1 3 5

之后在Flip得到

3 1 2 4 5

。。。

。。这样一直查询下去,O(n)的算法复杂度实现就能够了。



#include <iostream>
#include <algorithm>
#include <string>
#include <iterator>
#include <sstream>
#include <deque>
#include <vector>
#include <cstdio>
using namespace std; int main()
{
string line;
while(getline(cin,line))
{
int x;
deque<int> deq;
deque<int>::iterator iter;
istringstream ss(line);
while(ss>>x){
deq.push_front(x);
cout<<x<<" ";
}
cout<<endl;
for(iter = deq.begin();iter != deq.end();++iter){
deque<int>::iterator iMax = max_element(iter,deq.end());
if(iter != iMax){ //当前这个数的位置不应该待在这
if(iMax != deq.end()-1){ //最大数不再最前面(操作1)
reverse(iMax,deq.end());
cout<<distance(deq.begin(),iMax)+1<<' ';
}
reverse(iter,deq.end()); //操作2
cout<<distance(deq.begin(),iter)+1<<' ';
}
}
cout<<"0"<<endl;
}
return 0;
}









版权声明:本文博主原创文章。博客,未经同意不得转载。

uva Stacks of Flapjacks的更多相关文章

  1. UVA Stacks of Flapjacks 栈排序

    题意:给一个整数序列,输出每次反转的位置,输出0代表排序完成.给一个序列1 2 3 4 5,这5就是栈底,1是顶,底到顶的位置是从1~5,每次反转是指从左数第i个位置,将其及其左边所有的数字都反转,假 ...

  2. Uva 120 - Stacks of Flapjacks(构造法)

    UVA - 120  Stacks of Flapjacks Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld &a ...

  3. uva 120 stacks of flapjacks ——yhx

     Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data ...

  4. UVaOJ 120 - Stacks of Flapjacks

    120 - Stacks of Flapjacks 题目看了半天......英语啊!!! 好久没做题...循环输入数字都搞了半天...罪过啊!!! 还是C方便一点...其实C++应该更方便的...C+ ...

  5. 【思维】Stacks of Flapjacks

    [UVa120] Stacks of Flapjacks 算法入门经典第8章8-1 (P236) 题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列. 试题分析:从插入排序即可找到 ...

  6. Stacks of Flapjacks(栈)

     Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data ...

  7. Stacks of Flapjacks

    Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data s ...

  8. (白书训练计划)UVa 120 Stacks of Flapjacks(构造法)

    题目地址:UVa 120 水题. 从最大的開始移,每次都把大的先翻到最上面,再翻到以下. 代码例如以下: #include <iostream> #include <cstdio&g ...

  9. B - Stacks of Flapjacks UVA - 120

    BackgroundStacks and Queues are often considered the bread and butter of data structures and find us ...

随机推荐

  1. Hibernate学习之关系映射(转)

    一.一对多 "一对多"是最普遍的映射关系,简单来讲就如消费者与订单的关系.一对多:从消费者角的度来说一个消费者可以有多个订单,即为一对多.多对一:从订单的角度来说多个订单可以对应一 ...

  2. 在python文本编辑器里如何设置Tab为4个空格

    python中缩进一般为四个空格,我总结3种常用编辑器中种如何设置Tab键为四个空格 第一种:下载python3.5时自带de 一个IDLE编辑器 在Options选项下的Configure IDLE ...

  3. [C++]Infinite House of Pancakes——Google Code Jam 2015 Qualification Round

    Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...

  4. 生成输出url时,使用CSS来控制超链接的格式

    在前文<生成输出url>中的第5点,介绍了使用ActionLink生成输出url中指定html标签属性. 例如, 假设Global.asax中的路由定义为: public static v ...

  5. python基础学习笔记1

    一.字符串: 1.不可变性.分片赋值对于字符串是不合法的. 2.字符串格式化 % eg: print 'The price is: %d' % 30 print 'The price is: %.2f ...

  6. RedHat/CentOS发行版本号及内核版本号对照表

    RedHat/CentOS发行版本号及内核版本号对照表 : Redhat 9.0———————————————2.4.20-8RHEL 3 Update 8————————————2.4.21-47R ...

  7. 数学之路(3)-机器学习(3)-机器学习算法-SVM[7]

    SVM是新近出现的强大的数据挖掘工具,它在文本分类.手写文字识别.图像分类.生物序列分析等实际应用中表现出非常好的性能.SVM属于监督学习算法,样本以属性向量的形式提供,所以输入空间是Rn的子集. 图 ...

  8. 《windows程序设计》学习_3.4:实现雷区翻转

    #include<windows.h> #include "resource.h" LRESULT CALLBACK WndProc (HWND, UINT, WPAR ...

  9. linux下mysql出现Access denied for user 'root'@'localhost' (using password: YES)解决方法

    # /etc/init.d/mysql stop # mysqld_safe --user=mysql --skip-grant-tables --skip-networking & # my ...

  10. CSS–Some Structure

    Some Structure About CSS Layout Position,Layer[层次] Box Model Visual Formatting Model BFC[block forma ...