http://acm.hdu.edu.cn/showproblem.php?pid=5500

Reorder the Books

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 942    Accepted Submission(s): 519

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
 
Source

假设开始时所有的顺序都是乱的则乱码数为n(即需要移动的步数也为n),从最后一位往前找递减列

找到一个符合的递减序列的元素,乱码数就减少1

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm> using namespace std; const int N = ; int main()
{
int t, n, a[N];
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i = ; i <= n ; i++)
scanf("%d", &a[i]);
int x = n;
for(int i = n ; i >= ; i--)
{
if(a[i] == x)
x--;//乱码数
}
printf("%d\n", x);
}
return ;
}

hdu 5500 Reorder the Books的更多相关文章

  1. HDU 5500 Reorder the Books 贪心

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

  2. hdu 5500 Reorder the Books(规律)

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

  3. HDU 5500 Reorder the Books (水题)

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

  4. hdoj 5500 Reorder the Books

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

  5. 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 ...

  6. Reorder the Books -- hdu -- 5500

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

  7. Reorder the Books(规律)

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

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

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

  9. Reorder the Books-HDU5500

    Problem Description dxy has a collection of a series of books called "The Stories of SDOI" ...

随机推荐

  1. PTA 习题集5-18 打印选课学生名单(哈希)

    假设全校有最多40000名学生和最多2500门课程.现给出每个学生的选课清单,要求输出每门课的选课学生名单. 输入格式: 输入的第一行是两个正整数:N(≤40000),为全校学生总数:K(≤2500) ...

  2. 操作符重载operator

    发现一篇好文: 转载: http://www.cnblogs.com/xiehongfeng100/p/4040858.html #include <iostream>#include & ...

  3. mysql基本的增删改查和条件语句

    增 insert into 表名(列名,列名......) values("test1",23),("test2",23),("test3" ...

  4. 【SQL模板】三.插入/更新 数据模板TSQL

    ---Name: 插入/更新 数据模板.sql ---Purpose: 用于更新 数据库中 历史数据 或 插入 新数据 的脚本模板 ---Author: xx ---Time: 2015-12-18 ...

  5. pthread_exit pthread_join

    int pthread_join(pthread_t thread, void **retval); int pthread_detach(pthread_t thread); void pthrea ...

  6. BZOJ2424 [HAOI2010]订货 - 费用流

    题解 (非常裸的费用流 题意有一点表明不清: 该月卖出的商品可以不用算进仓库里面. 然后套上费用流模板 代码 #include<cstring> #include<queue> ...

  7. asp.net状态保持

    1.首先如果不是asp.net webform而只是一个纯粹的html页面和ashx一般处理程序的话,因为http协议的无状态,每一次的页面请求都会重新实例化一个页面对象(注意实例化页面对象其实是通过 ...

  8. C#集合类型大盘点(转载)

    原文地址:http://www.cnblogs.com/jesse2013/p/CollectionsInCSharp.html#b02

  9. smrtlink

    SMRT Link is the web-based end-to-end workflow manager for the Sequel™ System. It includes software ...

  10. 2018.10.04 NOIP模拟 K进制(模拟)

    传送门 签到题,直接瞎模拟就行了. 代码