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 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
Sourcebestcoder Round #59 (Div.1)
Assuming that all the order is chaotic at the beginning, the garbled number is n (that is, the number of steps that need to be moved is also N), from the last one forward to the descending column
Find an element of a conforming descending sequence, reduce the number of garbled characters by 1
#include <stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>#include<algorithm>using namespacestd;Const intN = -;intMain () {intT, N, A[n]; scanf ("%d", &t); while(t--) {scanf ("%d", &N); for(inti =1; I <= N; i++) scanf ("%d", &A[i]); intx =N; for(inti = n; I >=1; i--) { if(A[i] = =x) x--;//garbled number} printf ("%d\n", x); } return 0;}
HDU 5500 Reorder The Books