Reorder the Books (regular)

Source: Internet
Author: User

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 DESCRIPTIONDXY has a collection of a series of books called "The Stories of Sdoi", there isn(n≤) Books in the This series. Every book have a number from1ToN.

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 are allowed to touch them.

One day EVENSGN visited DXY's home, because DXY is dating with he girlfriend, DXY let EVENSGN stay at home himself. EVENSGN was curious about the this series of books. So he took a look at them. He found out there is a story about "Little e&little Q". While losing himself in the story,he disrupted the order of the books.

Knowing that DXY would is back SOON,EVENSGN needed to get the books ordered again. But because the books were too heavy. The only thing EVENSGN could do is 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 Calculate the minimum steps evensgn would use to reorder the books? If you could solve the problem for Him,he would give you a signed book "The Stories of sdoi 9:the stories of Little E" as a Gift.

Inputthere is several testcases.

There is an positive integerT(t≤) The first line standing for the number of testcases.

For each testcase, there was an positive integerNThe first line standing is the number of books in this series.

FollowedNPositive integers separated by space standing for the order of the disordered books,theItH Integer stands for theItH Book's number (from top to bottom).


Hint:
For the first testcase:moving in the order ofbok3,book2,book1 ,(4,1,2,3)→(3,4,1,2) → (2, 3,4 ,1 ) → ( 1,2 ,3 , 4) , and this was the best-of-the-reorder the books.
For the second Testcase:it ' s already ordered so there are no operation needed.

Outputfor each testcase,output one line for a integer standing for the minimum steps EVENSGN would use to reorder the Boo Ks.

Sample Input244 1 2 351 2 3 4 5

Sample OUTPUT3 0

Analysis: Each time the maximum number of books placed on the top, so that the maximum number of ans=, from the bottom of the search, if the current number equals the maximum number, you do not have to smoke, number minus one, the final number of the value is the minimum number of steps;

Code:

1#include <stdio.h>2 intMain () {3     intt,n,ans,m[ -];4scanf"%d",&T);5      while(t--){6scanf"%d",&N);7          for(intI=1; i<=n;i++) scanf ("%d", m+i);8ans=N;9          for(inti=n;i;i--){Ten             if(Ans==m[i]) ans--; One         } Aprintf"%d\n", ans); -     } -     return 0; the}

Reorder the Books (regular)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.