Booksort

Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 173    Accepted Submission(s): 81
Problem Description
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.

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?

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

 
Output
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".

 
Sample Input
3
6
1 3 4 6 2 5
5
5 4 3 2 1
10
6 8 5 3 4 7 2 9 1 10
 
Sample Output
2
3
5 or more
 
Source


题意:
给n本书,编号1-n,乱序的,现在可以任选连续的一摞书插入到任意位置,问能否在4步内使书编号升序。能输出最少步数,不能输出 5 or more。

感想:
虽然深度只有4层,但状态还是太多了,先尝试了一下dbfs,连最后一组都跑不出来,提交果断TLE。后来看了lcm博客发现是IDA*,哎,我的A*基本是空白,看了启发式函数之后才A的。A*很强大,以后要开始研究啦。

思路:
主要是找启发式函数,考虑到每次操作只会影响3个块的后继块是否正确,所以得到启发式函数f(s)=h(s)+3*g(s).黑书上P169有类似讲解,可供参考。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define maxn 16
using namespace std; int n,m,ans,depth,flag;
int a[maxn]; bool isok()
{
int i,j;
for(i=1;i<=n;i++)
{
if(a[i]!=i) return false ;
}
return true ;
}
int h()
{
int i,j,t=0;
for(i=0;i<=n;i++)
{
if(a[i]+1!=a[i+1]) t++;
}
return t;
}
void dfs(int d)
{
if(isok())
{
flag=1;
return ;
}
if(flag||(d-1)*3+h()>depth*3) return ; // 实现IDA*
int i,j,k,p;
int tmp[maxn];
for(i=1; i<=n; i++) // 模拟操作
{
for(j=i; j<=n; j++)
{
for(k=1; k<=i-1; k++)
{
memcpy(tmp,a,sizeof(tmp));
for(p=k; p<=i-1; p++)
{
a[p+j-i+1]=tmp[p];
}
for(p=i; p<=j; p++)
{
a[p-i+k]=tmp[p];
}
dfs(d+1);
memcpy(a,tmp,sizeof(tmp));
}
for(k=j+1; k<=n; k++)
{
memcpy(tmp,a,sizeof(tmp));
for(p=j+1; p<=k; p++)
{
a[p-j-1+i]=tmp[p];
}
for(p=i; p<=j; p++)
{
a[p+k-j]=tmp[p];
}
dfs(d+1);
memcpy(a,tmp,sizeof(tmp));
}
}
}
}
int main()
{
int i,j,t,flg;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
flg=1;
for(i=1; i<=n; i++)
{
scanf("%d",&a[i]);
if(a[i]!=i) flg=0;
}
a[0]=0,a[n+1]=n+1;
if(flg)
{
printf("0\n");
continue ;
}
flag=depth=0;
while(!flag)
{
depth++;
dfs(1);
if(depth>=4) break ;
}
if(flag) printf("%d\n",depth);
else printf("5 or more\n");
}
return 0;
}



 

hdu 1685 Booksort (IDA*)的更多相关文章

  1. POJ 1077 HDU 1043 Eight (IDA*)

    题意就不用再说明了吧......如此经典 之前想用双向广搜.a*来写,但总觉得无力,现在用IDA*感觉其他的解法都弱爆了..............想法活跃,时间,空间消耗很小,给它跪了 启发式搜索关 ...

  2. DNA sequence HDU - 1560(IDA*,迭代加深搜索)

    题目大意:有n个DNA序列,构造一个新的序列,使得这n个DNA序列都是它的子序列,然后输出最小长度. 题解:第一次接触IDA*算法,感觉~~好暴力!!思路:维护一个数组pos[i],表示第i个串该匹配 ...

  3. HDU 2485 Destroying the bus stations (IDA*+ BFS)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意:给你n个点,m条相连的边,问你最少去掉几个点使从1到n最小路径>=k,其中不能去掉1, ...

  4. HDU 1813 Escape from Tetris (IDA*)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1813 题意:给你一个n*n的迷宫,其中0代表有一个人在这个位置,1代表墙,现在要求一个路线,使所有的人通 ...

  5. POJ3460 Booksort(IDA*)

    POJ3460 Booksort 题意:给定一个长度为n的序列,每次可以取出其中的一段数,插入任意一个位置,问最少需要几次操作才能使整个序列变为1~n 思路:IDA*+迭代加深搜索 小技巧:将一段数插 ...

  6. hdu 2234(IDA*)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2234 思路:IDA*可以搞,借鉴的是大牛的启发式函数h(): 可以考虑把每一行上的数转化成相同的,或者 ...

  7. hdu 1667(IDA*)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1667 思路:大牛说是IDA*的入门题=.=构造h()=8-max(1,2,3);  max(1,2,3 ...

  8. HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

  9. HDU 1560 DNA sequence (IDA* 迭代加深 搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...

随机推荐

  1. 关于SVD(Singular Value Decomposition)的那些事儿

    SVD简介 SVD不仅是一个数学问题,在机器学习领域,有相当多的应用与奇异值都可以扯上关系,比如做feature reduction的PCA,做数据压缩(以图像压缩为代表)的算法,还有做搜索引擎语义层 ...

  2. 你好,C++(37)上车的人请买票!6.3.3 用虚函数实现多态

    6.3.3  用虚函数实现多态 在理解了面向对象的继承机制之后,我们知道了在大多数情况下派生类是基类的“一种”,就像“学生”是“人”类中的一种一样.既然“学生”是“人”的一种,那么在使用“人”这个概念 ...

  3. 你好,C++(8)如何表达那些始终保持不变的数据量?3.2.2 常量

    3.2.2  常量 与变量可以用在程序中表达那些可能会发生变化的数据量相对应地,在C++中,我们用常量来表达那些始终保持不变的数据量.简单来讲,就是程序中直接使用的数值.字符.字符串以及const关键 ...

  4. 下拉菜单选择(jQuery实现)

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  5. Tomcat部署多个项目及相关配置

    1.配置多个项目 在tomcat的conf目录下,找到server.xml,在其中添加<Host>节点即可进行多个项目的部署 <Host name="localhost&q ...

  6. 关于几种常用的Adapter使用区别

    Adapter常用的实现类如下: 1.ArrayAdapter:简单.易用的Adapter,通常用于将数组或List集合的多个值包装成多个列表项. 2.SimpleAdapter:并不简单.功能强大的 ...

  7. The formatter threw an exception while trying to deserialize the message in WCF

    有一个WCF应用, 主要功能是存储doc, txt等类型文件到database,当文件的大小在16kb之内,调用WCF service能正常工作:但如果文件大小超出16KB之外, 它将抛出这样一个错误 ...

  8. 你不知道的JavaScript(作用域和闭包)

    作用域和闭包 ・作用域 引擎:从头到尾负责整个JavaScript的编译及执行过程. 编译器:负责语法分析及代码生成等. 作用域:负责收集并维护由所有声明的标识符(变量)组成的一系列查询,并实施一套非 ...

  9. java 属性

    //非静态类 不能定义静态属性/方法/静态类, 可以定义静态常量属性. public class A{ public class B{ public static String  _str; //❌, ...

  10. 【Hybrid App】关于Hybrid App技术解决方案的选择

    [引言]近年来随着移动设备类型的变多,操作系统的变多,用户需求的增加,对于每个项目启动前,大家都会考虑到的成本,团队成员,技术成熟度,时间,项目需求等一堆的因素.因此,开发App的方案已经变得越来越多 ...