BestCoder Round 59 (HDOJ 5500) Reorder the Books
Problem Description
dxy has a collection of a series of books called “The Stories of SDOI”,There are n(n≤19) books in this series.Every book has a number from 1 to n.
dxy puts these books in a book stack with the order of their numbers increasing from top to bottom. dxy takes great care of these books and no one is allowed to touch them.
One day Evensgn visited dxy’s home, because dxy was dating with his girlfriend, dxy let Evensgn stay at home himself. Evensgn was curious about this series of books.So he took a look at them. He found out there was a story about “Little E&Little Q”. While losing himself in the story,he disrupted the order of the books.
Knowing that dxy would be back soon,Evensgn needed to get the books ordered again.But because the books were too heavy.The only thing Evensgn could do was to take out a book from the book stack and and put it at the stack top.
Give you the order of the disordered books.Could you calculate the minimum steps Evensgn would use to reorder the books? If you could solve the problem for him,he will give you a signed book “The Stories of SDOI 9: The Story of Little E” as a gift.
Input
There are several testcases.
There is an positive integer T(T≤30) in the first line standing for the number of testcases.
For each testcase, there is an positive integer n in the first line standing for the number of books in this series.
Followed n positive integers separated by space standing for the order of the disordered books,the ith integer stands for the ith book’s number(from top to bottom).
Hint:
For the first testcase:Moving in the order of book3,book2,book1 ,(4,1,2,3)→(3,4,1,2)→(2,3,4,1)→(1,2,3,4),and this is the best way to reorder the books.
For the second testcase:It’s already ordered so there is no operation needed.
Output
For each testcase,output one line for an integer standing for the minimum steps Evensgn would use to reorder the books.
Sample Input
2
4
4 1 2 3
5
1 2 3 4 5
Sample Output
3
0
Code
#include <iostream>
using namespace std;
int main()
{
int T; cin >> T;
int book[20];
while (T--)
{
int n; cin >> n;
int index, number = n;
for (int i = 0; i < n; i++)
{
cin >> book[i];
if (book[i] == n) index = i;
}
for (int i = index; i >= 0; i--)
{
if (book[i] == number) number--;
}
cout << number << endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
BestCoder Round 59 (HDOJ 5500) Reorder the Books的更多相关文章
- hdoj 5500 Reorder the Books
Reorder the Books Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- hdu 5500 Reorder the Books
http://acm.hdu.edu.cn/showproblem.php?pid=5500 Reorder the Books Time Limit: 4000/2000 MS (Java/Othe ...
- BestCoder Round #59 (div.2) B. Reorder the Books 想法题
Reorder the Books 问题描述 dxy家收藏了一套书,这套书叫<SDOI故事集>,<SDOI故事集>有n(n\leq 19)n(n≤19)本,每本书有一个编号,从 ...
- HDU 5500 Reorder the Books 贪心
Reorder the Books Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...
- hdu 5500 Reorder the Books(规律)
题意: 有一个1→n的排列形成的数列,我们要用最少的操作次数把这个数列从小到大排序,每次操作都是把一个数放到整个数列的最前面. 思路: 首先最大的数n是不用操作的(其他数操作好了,n ...
- HDU 5500 Reorder the Books (水题)
题意: 有n本书,编号为1~n,现在书的顺序乱了,要求恢复成有序的样子,每次只能抽出其中一本并插到最前面,问最少需要多少抽几次? 思路: 如果pos[i]放的不是书i的话,则书i的右边所有的书都必须抽 ...
- HDU5501/BestCoder Round #59 (div.2)The Highest Mark dp+贪心
The Highest Mark 问题描述 2045年的SD省队选拔,赛制和三十年前已是完全不同.一场比赛的比赛时间有 tt 分钟,有 nn 道题目. 第 ii 道题目的初始分值为 A_i(A_i \ ...
- Reorder the Books -- hdu -- 5500
http://acm.hdu.edu.cn/showproblem.php?pid=5500 Reorder the Books Time Limit: 4000/2000 MS (Java/Othe ...
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
随机推荐
- Js仿弹框
收藏一个简单实用的JS弹框,通过隐藏和显示div来实现,代码来自脚本之家! <html> <head> <title> LIGHTBOX EXAMPLE </ ...
- python学习第二课要点记录
字典使用时,使用for k,v in items():要将字典转换为元组,因此效率较低,如果数据量较大,就不建议使用这样的形式获取key和value的值,而要使用 for item in dict: ...
- Layer 一个让你想到即可做到的web弹窗/层 解决方案
最近工作上面用到的web弹窗组件layer layer是一款口碑极佳的web弹层组件,她具备全方位的解决方案,致力于服务各个水平段的开发人员,您的页面会轻松地拥有丰富而友好的操作体验. layer官方 ...
- Solr4.8.0源码分析(2)之Solr的启动(一)
上文写到Solr的启动过程是在SolrDispatchFilter的init()里实现,当Tomcat启动时候会自动调用init(); Solr的启动主要在 this.cores = createCo ...
- CSS中.和#区别
一.问题来源 制作导航栏,参考别人的代码,发现的. 二.解析 2.1 概述 id:用来定义页面中大的样式,如栏目划分,顶部,正文,底部等:用#top的形式来定义: class:用来定义一些比较细节的样 ...
- gnu cc扩展和ABI
gnc cc扩展标准c的语法,非常强大!!!详情请见: http://uw714doc.sco.com/cgi-bin/info2html?%28gcc.info%29C%2520Extensions ...
- Children of the Candy Corn (bfs+dfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8120 Accepted: 3547 Description The c ...
- COJ 0501 取数游戏(TPM)
取数游戏(TPM) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给你一个n*n的格子的棋盘,每个格子里面有一个非负数.从中取 ...
- (2015年郑州轻工业学院ACM校赛题) C 数列
在我们做完B题之后就去看C题了, 发现很多人都已经做出来了, 并且一血还是我们学弟拿的, 感觉这题不难, 我们举了几个例子之后发现全是Alice 然后我们就决定意淫一下,试试看! 没想到就A了 - . ...
- Linux 下 的 cc 和 gcc
在Linux下一会看到cc,另一会又看到gcc,感觉又点混乱的样子.它们是同一个东西么,有啥区别呢? 一分为二地看: 首先,如果讨论范围在Unix和Linux之间,那么cc和gcc不是同一个东西.cc ...