您的位置:首页 > 其它

HDU 5500 Reorder the Books

2015-10-11 10:37 393 查看

Reorder the Books

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

Total Submission(s): 218 Accepted Submission(s): 146



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each testcase,output one line for an integer standing for the minimum steps Evensgn would use to reorder the books.

[align=left]Sample Input[/align]

2
4
4 1 2 3
5
1 2 3 4 5


[align=left]Sample Output[/align]

3
0


[align=left]Source[/align]
BestCoder Round #59 (div.1)

[align=left]Recommend[/align]
hujie | We have carefully selected several similar problems for you: 5503 5502 5501 5499 5498

dxy家收藏了一套书,这套书叫《SDOI故事集》,《SDOI故事集》有n(n≤19)n(n\leq 19)n(n≤19)本,每本书有一个编号,从111号到nnn号。
dxy把这些书按编号从小到大,从上往下摞成一摞。dxy对这套书极其重视,不允许任何人动这套书。
有一天Evensgn到dxy家玩,dxy因为和妹子有约会,就让Evensgn自己待在他家。Evensgn对这套书非常好奇,偷偷的看了一下,结果发现这里面竟然有当年小E和小Q的故事。Evensgn看得出神,结果把一摞书的顺序打乱了。
眼看着dxy就要回来了,Evensgn需要尽快把这摞书恢复到原先排好序的状态。由于每本书都非常重,所以Evensgn能做的操作只有把一本书从书堆中抽出来,然后把这本书放到书堆的顶部。
给你打乱的书的顺序,你能帮Evensgn算算最少需要几次上述的操作,他才能把这套书恢复顺序?假如你能算出来的话,Evensgn答应送给你一本他签名的书《SDOI故事集9:小E的故事》

输入描述
输入包含多组数据。
第一行包含一个正整数T(T≤30)T(T\leq 30)T(T≤30)表示数据组数。
对于每组数据,第一行为一个正整数nnn表示这套《SDOI故事集》中有多少本书。
接下来一行nnn个用空格分开的正整数,表示Evensgn打乱后的这摞书的书号顺序(从上往下)。

输出描述
对于每组数据,输出一行一个整数,表示Evensgn最少需要几次操作才能讲书恢复顺序。


思路:

从大到小找,看该值前面是否存在比他大的值,存在将该值置前,不存在,继续遍历下一个值.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

int a[1000];
int main()
{
int T,n;
while(~scanf("%d",&T))
{
while(T--)
{
vector<int> Q;  <span id="transmark"></span>//清空
scanf("%d",&n);
for(int i=0; i<n; i++)
{
int t;
scanf("%d",&t);
a[i]=t;
Q.push_back(t);
}
sort(a,a+n);
vector<int>::iterator it;
int num=0;
for(int i=n-1;i>=0;i--)
{
for(it=Q.begin();*it!=a[i];it++)
{
if(*it>a[i])
{
Q.erase(it);
Q.insert(Q.begin(),a[i]);
num++;
break;
}
}
}
printf("%d\n",num);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: