HDU 5500 Reorder the Books(O(n)的复杂度就够了)——BestCoder Round #59(div.1 div.2)

来源:互联网 发布:windows vista 真漂亮 编辑:程序博客网 时间:2024/05/16 01:31

Reorder the Books

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)


Problem Description
dxy has a collection of a series of books called "The Stories of SDOI",There are n(n19) 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(T30) 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
244 1 2 351 2 3 4 5
 

Sample Output
30
 

Source
BestCoder Round #59 (div.1)
 

/************************************************************************/

附上该题对应的中文题

Reorder the Books

 
 
 Time Limit: 4000/2000 MS (Java/Others)
 
 Memory Limit: 131072/131072 K (Java/Others)
问题描述
dxy家收藏了一套书,这套书叫《SDOI故事集》,《SDOI故事集》有n(n\leq 19)n(n19)本,每本书有一个编号,从11号到nn号。dxy把这些书按编号从小到大,从上往下摞成一摞。dxy对这套书极其重视,不允许任何人动这套书。有一天Evensgn到dxy家玩,dxy因为和妹子有约会,就让Evensgn自己待在他家。Evensgn对这套书非常好奇,偷偷的看了一下,结果发现这里面竟然有当年小E和小Q的故事。Evensgn看得出神,结果把一摞书的顺序打乱了。眼看着dxy就要回来了,Evensgn需要尽快把这摞书恢复到原先排好序的状态。由于每本书都非常重,所以Evensgn能做的操作只有把一本书从书堆中抽出来,然后把这本书放到书堆的顶部。给你打乱的书的顺序,你能帮Evensgn算算最少需要几次上述的操作,他才能把这套书恢复顺序?假如你能算出来的话,Evensgn答应送给你一本他签名的书《SDOI故事集9:小E的故事》
输入描述
输入包含多组数据。第一行包含一个正整数T(T\leq 30)T(T30)表示数据组数。对于每组数据,第一行为一个正整数nn表示这套《SDOI故事集》中有多少本书。接下来一行nn个用空格分开的正整数,表示Evensgn打乱后的这摞书的书号顺序(从上往下)。
输出描述
对于每组数据,输出一行一个整数,表示Evensgn最少需要几次操作才能讲书恢复顺序。
输入样例
244 1 2 351 2 3 4 5
输出样例
30
Hint
对于第一组数据,我们先把33号书放到最上面,接着操作22号书,最后操作11号书,(4,1,2,3)\rightarrow (3,4,1,2)\rightarrow(2,3,4,1)\rightarrow(1,2,3,4)(4,1,2,3)(3,4,1,2)(2,3,4,1)(1,2,3,4),这样就有序了对于第二组数据,这摞书本来就有序了,所以不需要任何操作n>15n>15的数据不超过1010
/****************************************************/

出题人的解题思路:

Reorder the Books

By davidlee1999WTK

把这题的模型简化一下,有一个1\rightarrow n1n的排列形成的数列,我们要用最少的操作次数把这个数列排序,每次操作都是把一个数放到整个数列的最前面。 首先我们可以注意到每个数最多只会被操作一次。因为假如有一个数被往前拿了两次,显然第一次的操作是没有意义的。 然后能发现一定先操作大的数,再操作小的数。因为假如先把小的数放前面去了,再把大的数放前面去,小的数就又在大的数后面了,小的数必定还得再操作一次,然而操作两次是不划算的。

到这里,对于19的数据范围,我们有一个很暴力的做法,2^n2n枚举要操作哪些数,这些操作按数的大小从大往小的顺序,模拟一下,然后检查一下最后的序列是否有序,复杂度O(n*2^n)O(n2n)

我们很快又能发现,假如操作了大小等于kk的数,那么所有小于kk的数也都得操作了。所以我们不用2^n枚举,直接mm11开始从小到大枚举,表示要操作前mm小的数,然后模拟,验证,这样复杂度为O(n^2)O(n2)

不过其实mm也是不用枚举的。 首先可以发现最大的数nn是不用操作的(其他数操作好了,数"nn"自然就在最后面了)。 于是我们先找到最大的数"nn"的位置,从这个位置往前找,直到找到(n-1)(n1)。假如找到头也没找到(n-1)(n1),那么数"(n-1)(n1)"需要操作,而一旦操作了(n-1)(n1),根据前面结论,总共就需要(n-1)(n1)次操作了;假如找到了(n-1),那么数"(n-1)(n1)"也不需要操作(和数"nn"不需要操作一个道理)。 同理,我们接着从(n-1)(n1)的位置往前找(n-2)(n2),再从(n-2)(n2)的位置往前找(n-3)(n3)...假如数kk找不到了,那么就至少需要kk次操作。这种做法的复杂度是O(n)O(n)

此题最简单的方法O(n)就能实现,对于打乱的编号,如下图
我们每次可以从中抽出一个编号放在栈的顶部,使得最终从顶至底编号递增

初看可能没有什么思路,但是多列举点数据,就会发现,从底部开始,原本满足序列的可以不再改动,一直找到顶部之后,不在原先序列上的数的个数,就是我们要操作的次数。
就拿上述例子来说明,从底部往上找,5已经在最底下,接下来应该是4,那4与5之间的3必定要操作的,但是什么时候操作就不重要了,一直走到顶部之后,只有4和5是在递增序列上的,故至少要操作3次
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<cmath>#include<string>#include<algorithm>#include<iostream>#define exp 1e-10using namespace std;const int N = 30;const int inf = 1000000000;const int mod = 2009;int s[N];int main(){    int t,k,n,i;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(i=1;i<=n;i++)            scanf("%d",&s[i]);        for(k=i=n;i>0;i--)            if(k==s[i])                k--;        printf("%d\n",k);    }    return 0;}
菜鸟成长记
0 0