Booksort[POJ3460]

版权声明:作为一个蒟蒻,转载时请通知我这个蒟蒻 https://blog.csdn.net/zyszlb2003/article/details/89438321

欢迎大家访问我的老师的OJ———caioj.cn

题面描述

The Leiden University Library has millions of books. When a student wants to borrow a certain book, he usually submits an online loan form. If the book is available, then the next day the student can go and get it at the loan counter. This is the modern way of borrowing books at the library.

There is one department in the library, full of bookcases, where still the old way of borrowing is in use. Students can simply walk around there, pick out the books they like and, after registration, take them home for at most three weeks.

Quite often, however, it happens that a student takes a book from the shelf, takes a closer look at it, decides that he does not want to read it, and puts it back. Unfortunately, not all students are very careful with this last step. Although each book has a unique identification code, by which the books are sorted in the bookcase, some students put back the books they have considered at the wrong place. They do put it back onto the right shelf. However, not at the right position on the shelf.

Other students use the unique identification code (which they can find in an online catalogue) to find the books they want to borrow. For them, it is important that the books are really sorted on this code. Also for the librarian, it is important that the books are sorted. It makes it much easier to check if perhaps some books are stolen: not borrowed, but yet missing.

Therefore, every week, the librarian makes a round through the department and sorts the books on every shelf. Sorting one shelf is doable, but still quite some work. The librarian has considered several algorithms for it, and decided that the easiest way for him to sort the books on a shelf, is by sorting by transpositions: as long as the books are not sorted,

take out a block of books (a number of books standing next to each other),
shift another block of books from the left or the right of the resulting ‘hole’, into this hole,
and put back the first block of books into the hole left open by the second block.
One such sequence of steps is called a transposition.

The following picture may clarify the steps of the algorithm, where X denotes the first block of books, and Y denotes the second block.

Original situation:
After step 1:
After step 2:
After step 3:
Of course, the librarian wants to minimize the work he has to do. That is, for every bookshelf, he wants to minimize the number of transpositions he must carry out to sort the books. In particular, he wants to know if the books on the shelf can be sorted by at most 4 transpositions. Can you tell him?

输入
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

One line with one integer n with 1 ≤ n ≤ 15: the number of books on a certain shelf.
One line with the n integers 1, 2, …, n in some order, separated by single spaces: the unique identification codes of the n books in their current order on the shelf.
输出
For every test case in the input file, the output should contain a single line, containing:

if the minimal number of transpositions to sort the books on their unique identification codes (in increasing order) is T ≤ 4, then this minimal number T;
if at least 5 transpositions are needed to sort the books, then the message “5 or more”.

思路

本题其实可以说就是通过移动区间 [ l 1 , r 1 ] [l_1,r_1] [ l 2 , r 2 ] [l_2,r_2] ,使整个数列不存在逆序对。
直接 BFS \operatorname{BFS} ?,相信如果这样能 A C AC 的话,那你就不会点进来了。
双向 BFS \operatorname{ BFS} 其实可以过。但在这里不赘叙了。
主要介绍的是 IDA* \operatorname{IDA*}
IDA* \operatorname{IDA*} 实质上就是迭代加深搜索+估计函数 f \operatorname{f}
本题的 IDA* \operatorname{IDA*} 可以通过目测得出。
首先我们先限制搜索深度 dep \operatorname{dep}
由于观察自己模拟可以发现,我们一次最多只能更改3本书的后继。
所以当我们每次统计 cnt \operatorname{cnt} ,即有多少错误后继时,应该将 cnt/3 \operatorname{cnt/3} ,得到估计函数 f \operatorname{f} .
由于这样会丢精度,所以我们可以得到这个柿子:
now*3+cnt≤dep*3 \operatorname{now*3+cnt \le dep*3}

只有当我们满足这个条件时,才能继续往下搜索。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;
const int N=20;
int a[N],n,dep;
int gj()
{
	int cnt=0;
	for(int i=1;i<n;i++)
	{
		if(a[i]+1!=a[i+1])cnt++;
	}
	if(a[n]!=n)return cnt+1;
	return cnt;
}
void work(int l,int r,int t)
{
	int p=r,b[N];
	for(int i=l;i<=t;i++)
	{
		b[i]=a[++p];
		if(p==t)p=l-1;
	}
	for(int i=l;i<=t;i++)a[i]=b[i];
}
bool dfs(int now)
{
	int cnt=gj();
	if(!cnt)return 1;
	if(3*now+cnt>3*dep)return 0;
	int c[N];memcpy(c,a,sizeof(a));
	for(int l=1;l<=n;l++)
		for(int r=l;r<=n;r++)
			for(int t=r+1;t<=n;t++)
			{
				work(l,r,t);//l,t为区间,r,r+1为断点 
				if(dfs(now+1))return 1;
				memcpy(a,c,sizeof(c));
			}
	return 0;
}
void booksort()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	dep=0;
	while(dep<=4)
	{
		if(dfs(0)){printf("%d\n",dep);return ;}
		dep++;
	}
	puts("5 or more");
}
int main()
{
	int t;scanf("%d",&t);
	while(t--)booksort();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zyszlb2003/article/details/89438321
POJ