Reorder the Books
Time limit:4000/2000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 919 Accepted Submission (s): 501
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 Output30 Test Instructions: A stack of books order disorderly, let you according to order from small to large sequence to arrange it neatly, one can only take a book, and must be placed on the top, ask at least how many steps can be solved: first calculate the number of books that have been sorted in order sum (note that this sequence is not necessarily continuous, and must be a sequence from a number to N, minus sum by n
#include <stdio.h> #include <string.h> #include <algorithm> #define MAX 100100using namespace Std;int A [Max];int Main () { int n,m,t,i,j; scanf ("%d", &t); int Maxx,ans; while (t--) { scanf ("%d", &n); maxx=0; for (i=1;i<=n;i++) { scanf ("%d", &a[i]); if (Maxx<a[i]) { maxx=a[i]; ans=i; } } int ant=ans,sum=0; for (j=ans;j>=1;j--) { if (a[j]==a[ant]-1) { sum++; ant=j; } } printf ("%d\n", n-sum-1); } return 0;}
Hdoj 5500 Reorder The Books