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的更多相关文章

  1. hdoj 5500 Reorder the Books

    Reorder the Books Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  2. hdu 5500 Reorder the Books

    http://acm.hdu.edu.cn/showproblem.php?pid=5500 Reorder the Books Time Limit: 4000/2000 MS (Java/Othe ...

  3. BestCoder Round #59 (div.2) B. Reorder the Books 想法题

    Reorder the Books 问题描述 dxy家收藏了一套书,这套书叫<SDOI故事集>,<SDOI故事集>有n(n\leq 19)n(n≤19)本,每本书有一个编号,从 ...

  4. HDU 5500 Reorder the Books 贪心

    Reorder the Books Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  5. hdu 5500 Reorder the Books(规律)

    题意:   有一个1→n的排列形成的数列,我们要用最少的操作次数把这个数列从小到大排序,每次操作都是把一个数放到整个数列的最前面. 思路:        首先最大的数n是不用操作的(其他数操作好了,n ...

  6. HDU 5500 Reorder the Books (水题)

    题意: 有n本书,编号为1~n,现在书的顺序乱了,要求恢复成有序的样子,每次只能抽出其中一本并插到最前面,问最少需要多少抽几次? 思路: 如果pos[i]放的不是书i的话,则书i的右边所有的书都必须抽 ...

  7. HDU5501/BestCoder Round #59 (div.2)The Highest Mark dp+贪心

    The Highest Mark 问题描述 2045年的SD省队选拔,赛制和三十年前已是完全不同.一场比赛的比赛时间有 tt 分钟,有 nn 道题目. 第 ii 道题目的初始分值为 A_i(A_i \ ...

  8. Reorder the Books -- hdu -- 5500

    http://acm.hdu.edu.cn/showproblem.php?pid=5500 Reorder the Books Time Limit: 4000/2000 MS (Java/Othe ...

  9. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

随机推荐

  1. 织梦dede标签tags的美化教程

    我们在行dede仿站的时候,经常会遇到tags标签的调用,调用非常简单,官方有专门的调用标签,但是官方的调用是一成不变的模式,dede5.6以前的版本,基本上都是黑色相同大小的表示,而在dede5.7 ...

  2. 转:Python 与 Excel 不得不说的事

    数据处理是 Python 的一大应用场景,而 Excel 则是最流行的数据处理软件.因此用 Python 进行数据相关的工作时,难免要和 Excel 打交道. 如果仅仅是要以表单形式保存数据,可以借助 ...

  3. IOS制作一个漂亮的登录界面

    上图是Facebook的登录界面,看起来很漂亮,eamil框和passwod框合在一起,那么这种效果是怎么做出来的呢?我们都知道输入框用layer属性是可以做成圆角的形式,那么怎么样才能够仅仅只让上边 ...

  4. 欧几里德算法gcd及其拓展终极解释

    这个困扰了自己好久,终于找到了解释,还有自己改动了一点点,耐心看完一定能加深理解   扩展欧几里德算法-求解不定方程,线性同余方程. 设过s步后两青蛙相遇,则必满足以下等式: (x+m*s)-(y+n ...

  5. Boost使用笔记(Smart_ptr)

    我是Word写的,复制过来实在懒得在排版了,有兴趣的朋友可以去我的百度文库看,谢谢 http://wenku.baidu.com/view/34e485e2f61fb7360b4c653e.html ...

  6. [转载]如何查看某个查询使用了多少TempDB空间

    http://www.cnblogs.com/CareySon/p/3910337.html 通过下面脚本可以查看某个查询实用的TempDB的空间. 第一步是查询出当下TempDB实用空间是多少, 第 ...

  7. ELK beats平台介绍

    原文链接:http://www.tuicool.com/articles/mYjYRb6 beats是一个代理,将不同类型的数据发送到elasticsearch.beats可以直接将数据发送到elas ...

  8. Android 4.4 上实现透明导航栏和状态栏 Translucent system bar

    Translucent system UI styling To get the most impact out of your content, you can now use new window ...

  9. COJN 0487 800301红与黑

    800301红与黑 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖. ...

  10. 第一个Windows程序

    今天,我们的任务就是和大家一起开发第一个Windows程序,这个程序的功能非常简单,就是弹出一个对话框,但是简单的程序可以帮助大家建立信心. 例1 第一个Windows程序 /* ********** ...