BestCoder Round #59 Reorder the Books
来源:互联网 发布:informatica linux 编辑:程序博客网 时间:2024/06/11 07:47
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5500
题目又臭又长……题意是说,一堆书本来是按编号从上往下依次增大的顺序堆好的,现在被打乱了,我们每次能操作的是从中间抽出一本书放到最上面,问我们最少需要的操作数。
当然,因为n最大才19,你当然可以去暴力求解,暴力深搜绝对能够搞定的,但是那么这么一道想法题便数去了意义,我们得去认真思考才能有所收获,我们先,仔细想想,那些书是我们必须要移动才行的呢,首先小编先给出一个例子(样例实在是没什么参考价值),1,4,3,2,5,我们要达到的状态呢就是1,2,3,4,5,因为我们每一次操作都是把抽出来的放在最上面,那么我们就从最下面开始思考,就这个样例而言,最后4是在5的上面的,那么不难想到,这中间的书一定是会被移动的,不然没法达到这个状态,再仔细思考,4的前面是1,但是最终状态4的前面应该是3,那么中间的数肯定又会被移动,所以这个1是一定会被移动的。再思考,就这个样例而言肯定是先移动2,再移动3,因为如果我们先移动小的,再移动大的,我们又会再移动一次,所以中间有几个我们就移几次,因为这个顺序我们都是能控制好的,所以我们按从大到小的顺序,从下面往上面扫,不是按顺序的操作数就+1,就能得到我们的答案。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int a[20];int main(){int T;scanf("%d",&T);while(T--){int n;scanf("%d",&n);for(int i=0; i<n; i++)scanf("%d",&a[i]);int num = 0,Max = n;for(int i=n-1; i>=0; i--)if(a[i] == Max)Max--;else num++;printf("%d\n",num);}}
1 0
- BestCoder Round #59 Reorder the Books
- BestCoder Round #59 (div.2) 1002 Reorder the Books
- BestCoder Round #59 (div.2) HDU5500 Reorder the Books
- BestCoder Round #59 (div.2)B.Reorder the Books
- hdu 5500 Reorder the Books 【BestCoder Round #59 (div.2) 第二题】
- HDU 5500 Reorder the Books(O(n)的复杂度就够了)——BestCoder Round #59(div.1 div.2)
- BC#59 Reorder the Books
- HDU_5500 Reorder the Books
- hdu5500--Reorder the Books
- hdu5500 Reorder the Books
- HDU Reorder the Books
- hdu5500 Reorder the Books
- Reorder the Books(模拟)
- hdu5500 Reorder the Books 贪心
- HDU 5500 Reorder the Books
- HDU 5500 Reorder the Books
- hdu 5500 Reorder the books
- hdu 5500 Reorder the Books
- poj 1065
- 利用xtrabackup工具在线建立复制环境
- Metal 着色语言编程指南 二一
- linux信息查看,dmesg命令
- ubuntu14.04 mongodb c++ driver安装
- BestCoder Round #59 Reorder the Books
- SGU 169 Numbers
- 48.构建乘积数组(不能用除法)
- POJ 2393 Yogurt factory (贪心)
- 数据库的原理,一篇文章搞定(一)
- poj 1182 食物链 带权并查集
- HDU 5616 枚举子集
- TCMalloc 安装和使用
- HDUOJ 最小公倍数1108