hdu 5500 Reorder the Books(模拟)
来源:互联网 发布:控制台java 编辑:程序博客网 时间:2024/05/19 23:04
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5500
题目大意:
有一堆书,刚开始顺序是打乱的,现在要把他们重新整理为有序的。但是我每次只能从中间的书中抽一本放到顶部,问最少这样操作几次能够达到目的。
思路:
可以知道序号最大的那本书是不需要动的。然后我只需要找第n-1本书是否在n的前面,如果在前面就不需要动,继续看n-1本书,否则就要操作一次。一旦将n-1操作了,所有n-2本书都在他下面了,那么就需要n-2次操作了。所以这样总的操作数就知道了。
代码:
#include<stdio.h>#include<string.h>#define max(a,b) a>b?a:b#define min(a,b) a<b?a:bint main(){ int T,i,j,k,n,a[34],maxi,t,x,ans,h[34]; scanf("%d",&T); while(T--) { t=0; maxi=1; x=1; memset(h,0,sizeof(h)); scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&a[i]); h[a[i]]=i; //记录所有数的位置。 } ans=0; for(i=n;i>=1;i--) { if(h[i]<h[i-1]){ ans=i-1; break; } } printf("%d\n",ans); }}
0 0
- hdu 5500 Reorder the Books(模拟)
- HDU 5500 Reorder the Books(模拟)
- HDU 5500 - Reorder the Books(模拟)
- HDU 5500 Reorder the Books
- HDU 5500 Reorder the Books
- hdu 5500 Reorder the books
- hdu 5500 Reorder the Books
- HDU 5500 Reorder the Books
- hdu 5500 Reorder the Books
- HDU 5500 Reorder the Books
- Reorder the Books HDU - 5500
- HDU 5500 Reorder the Books
- HDU 5500 Reorder the Books
- HDU Reorder the Books
- Reorder the Books-HDU-5500-BC59div2
- hdu acm 5500 Reorder the Books
- hdu 5500 Reorder the Books(贪心)
- HDU 5500:Reorder the Books【规律】
- 数据持久化的方法
- ios之单例模式
- [51MCU] Chapter1:reg51.h
- 如何做好测试管理工作
- leetcode 25:Reverse Nodes in k-Group(15-10-11)
- hdu 5500 Reorder the Books(模拟)
- ubuntu下设置一个wifi热点
- 二、机器学习系统设计笔记之对真实样本分类
- Qt中实现删除文件某一行内容
- 欢迎使用CSDN-markdown编辑器
- dump pcm数据和一些备忘
- 三、机器学习系统设计笔记之聚类
- Fedora安装Texlive2013时出现Can't locate Digest/MD5.pm的解决方法
- C# Lambda表达式