Reorder the Books

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

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

分析:每次取最大编号的书籍放在最上面,令ans=最大编号,从下往上找,如果当前编号等于最大编号,就不用抽了,编号数减一,最终编号的值就是最小步数;

代码:

 #include<stdio.h>
int main(){
int T,N,ans,m[];
scanf("%d",&T);
while(T--){
scanf("%d",&N);
for(int i=;i<=N;i++)scanf("%d",m+i);
ans=N;
for(int i=N;i;i--){
if(ans==m[i])ans--;
}
printf("%d\n",ans);
}
return ;
}

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 贪心

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

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

  4. Reorder the Books -- hdu -- 5500

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

  5. hdu 5500 Reorder the Books

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

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

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

  7. hdu 5500 Reorder the Books(规律)

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

  8. HDU 5500 Reorder the Books (水题)

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

  9. Reorder the Books-HDU5500

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

随机推荐

  1. 用CSS3绘制图形

    参考资料:http://blog.csdn.net/fense_520/article/details/37892507 本文非转载,为个人原创,转载请先联系博主,谢谢~ 准备: <!DOCTY ...

  2. python3 模拟登录网站

    最近学习python,因经常登录公积金网站查看公积金缴存还款情况,so网上找了写脚本,修改了一下,方便获取网页中的数据. 使用谷歌浏览器F12查看登录请求内容 1.request header需要参数 ...

  3. RelativeLayout布局下实现控件平分空间

    起源:使用惯LinearLayout的朋友都知道,若想实现对屏幕的等分,只需要设置Layout_weight的值即可. 可是在RelativeLayout布局下实现等分却不是那么容易. 下面就简单介绍 ...

  4. OpenCV学习 4:摄像头视频读写与边缘检测

    原创文章,欢迎转载,转载请注明出处 想实现人脸识别,车辆识别,车牌识别.一般我们可不是读硬盘里面的视频文件的,都是直接从摄像头读取视频流然后直接识别的.所以读取摄像头的视频流这是基础...OpenCV ...

  5. android process bar 几种style

    process bar 圆形style记录 1. style="?android:attr/progressBarStyleSmall" //根据主题变颜色 2. holo 主题下 ...

  6. IP地址获取到为0:0:0:0:0:0:0:1

    引用 13 楼 oXiaoShe 的回复: Quote: 引用 11 楼 ahjsdzm 的回复: [Quote=引用 9 楼 huazaiyou 的回复:]最近在进行web开发时,遇到了reques ...

  7. 字符串比较必须使用strcmp

    char s1[]="this" char *s2 = "this" if(s1=="this"){ printf("s1 is ...

  8. poj2328---"right on"进入下一个case的模板(while)

    #include <stdio.h> #include <stdlib.h> #include<string.h> int main() { ]; ,end=; w ...

  9. javascript数组去重算法-----4

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. js判断终端是手机还是电脑

    $(function(){ function browserRedirect() { var sUserAgent= navigator.userAgent.toLowerCase(); var bI ...