当前位置: 代码迷 >> 综合 >> hdu5500 Reorder the Books (思维+模拟)
  详细解决方案

hdu5500 Reorder the Books (思维+模拟)

热度:48   发布时间:2024-01-12 20:35:41.0

题目链接:

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): 1822    Accepted Submission(s): 937


 

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)

Recommend

hujie

 题目大意:

给你n本编号为1-n的书,但是编号乱了,恢复原先的顺序需要移动多少次。;

移动规则:可以把任意位置的书拿出来,放到最前方

思路:

编号最大的书不需要移动,从编号最大往前数,从编号最大开始严格递减的(公差为一,可以不相邻)的书不需要移动,其余的只需要移动一次就好

记录下不需要移动的,n减去就是结果

This is the code

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
int a[50];
int main()
{int t;scanf("%d",&t);while(t--){int n;scanf("%d",&n);for(int i=1; i<=n; ++i)scanf("%d",&a[i]);int pos=-1;for(int i=1;i<=n;++i)//寻找n的位置if(a[i]==n)pos=i;int cnt=1;int ans=n;for(int i=pos-1;i>=1;--i)//寻找多少严格递减的{if(ans-a[i]==1){++cnt;ans=a[i];}}printf("%d\n",n-cnt);}return 0;
}