hdu5500 Reorder the Books (枚举+模拟)

来源:互联网 发布:mac 最好的输入法 编辑:程序博客网 时间:2024/05/07 16:35

Problem Description
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.

Input
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.

Output
For each testcase,output one line for an integer standing for the minimum steps Evensgn would use to reorder the books.

Sample Input
2
4
4 1 2 3
5
1 2 3 4 5

Sample Output
3
0

Source
BestCoder Round #59 (div.1)

题意:给出一个打乱的编号,然后进行操作,操作只能是把某个数拿到最前面。问最少多少次操作后,这个序列是从小到大排列的。
解法:N<=19….数据范围较小可以直接枚举,模拟。
首先我们可以注意到每个数最多只会被操作一次。因为假如有一个数被往前拿了两次,显然第一次的操作是没有意义的。 然后能发现一定先操作大的数,再操作小的数。因为假如先把小的数放前面去了,再把大的数放前面去,小的数就又在大的数后面了,小的数必定还得再操作一次,然而操作两次是不划算的。

代码1:从最大的数开始枚举,给一个i 找出位置x,然后找出i-1的数的位置y,如果有y

#include<cstdio>#include<iostream>#include<algorithm>using namespace std;int main(){    int O_O;    cin>>O_O;    while(O_O--)    {        int n;        int a[30];        cin>>n;        for(int i=1;i<=n;i++) cin>>a[i];        int ans=0;        for(int i=n;i>1;i--)        {            int x,y;            for(int j=1;j<=n;j++)//da            {                if(a[j]==i)                {                    x=j;                    break;                }            }            for(int j=1;j<=n;j++)//xiao            {                if(a[j]==i-1)                {                    y=j;                    break;                }            }            if(y>x)            {                for(int j=y;j>1;j--) a[j]=a[j-1];                a[1]=i-1;                ans++;            }           // for(int j=1;j<=n;j++) cout<<("%d ",a[j]);           // cout<<endl;        }        printf("%d\n",ans);    }    return 0;}又发现了另一个什么的写法:代码2:先假设操作数为n.其实意思与上边的写法意思类似,这里找出了既然y<x这是不需要移动i-1元素的,就算移动也是放到最前面。怎么操作都不会影响原序列中y<x。没找到一个就减去一个1。所以直接找出来就行。。。。至于他的写法。。for(int i=n;i>=1;i--) {  if(a[i]==ans) ans--; }这里就是估计的好好想想才能明白。。。他就是从大到小的位置去枚举,找到数ans。。(也就是(x)),然后又ans--;到下一个数。。。(如果有y>x,不管。因为只要找y<x。。。)......啊!感觉有点讲不清楚啊!让我在想想怎么说!(或许已经明白了,不用我说!)#include<cstdio>#include<iostream>#include<algorithm>using namespace std;/*int main(){    int O_O;    cin>>O_O;    while(O_O--)    {        int n;        int a[30];        cin>>n;        for(int i=1;i<=n;i++) cin>>a[i];        int ans=n;        for(int i=n;i>=1;i--)        {            if(a[i]==ans) ans--;        }        printf("%d\n",ans);    }}
0 0
原创粉丝点击