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,

  1. take out a block of books (a number of books standing next to each other),
  2. shift another block of books from the left or the right of the resulting ‘hole’, into this hole,
  3. 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?

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

题意:给n本书,每次可以选取其中一段移动到另外的位置,使得书的编号变成1~n,如果步数超过4,就输出5 or more,反则输出步数。

思路:抽取书同一长度的书,有n-len+1种选择,有n-len个位置可以插入,并且因为一位置len长度后移等于后面位置前移

Σ(n-len)*(n-len+1)/2 <= (15*14+14*13+...+2*1)/2 == 560 ,因为只用判断是否步数<=4,不同搜索(560)4,时间复杂度过大。

通过IDA*(迭代加深+A*)

迭代加深:因为我们确定答案会在5之前收到

A*:评估函数,因为一次移动,最多可以使得3本书后面的书籍编号改变,所以该状态到达终态的至少步数f = ⌈错误后继/3⌉

(红色为三本书,是一次移动种最多改变的三本,橙色区域代表移动的区域,移动至第一个红色书的后面,那么第二个和第三个红色书的后继都改变了)

#include<iostream>
#include<cstdio>
#include<cmath> using namespace std; const int maxn = ;
int t;
int n; struct Node
{
int s[maxn];
}; bool check(int s[])
{
for(int i=; i<n; i++)
if(s[i] + != s[i+])
return ;
return ;
} Node change(Node x,int l,int r,int len)
{
Node tmp = x;
for(int i=l; i<l+len; i++)
x.s[r-len-l+i+] = tmp.s[i];
for(int i=l+len; i<=r; i++)
x.s[i-len] = tmp.s[i];
return x;
} int cal(int s[])
{
int ans = ;
for(int i=; i<n; i++)
if(s[i] + != s[i+])
ans++;
return ceil(ans/3.0);
} bool dfs(Node x,int now,int lim)
{
if(now + cal(x.s) > lim)
return ;
if(check(x.s))return ;
for(int len=; len<n; len++)
{
for(int i=; i+len- <=n; i++)
{
for(int j=i+len; j<=n; j++)
{
if(dfs(change(x,i,j,len),now+,lim))return ;
}
}
}
return ;
} int main()
{
scanf("%d",&t);
Node start;
while(t--)
{
scanf("%d",&n);
for(int i=; i<=n; i++)
scanf("%d",&start.s[i]);
for(int i=; i<; i++)
{
if(dfs(start,,i))
{
printf("%d\n",i);
break;
}
else if(i == )printf("5 or more\n"); }
}
}

Booksort POJ - 3460 (IDA*)的更多相关文章

  1. POJ题目(转)

    http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj29 ...

  2. Repeater POJ - 3768 (分形)

    Repeater POJ - 3768 Harmony is indispensible in our daily life and no one can live without it----may ...

  3. UVA - 10384 The Wall Pusher(推门游戏)(IDA*)

    题意:从起点出发,可向东南西北4个方向走,如果前面没有墙则可走:如果前面只有一堵墙,则可将墙向前推一格,其余情况不可推动,且不能推动游戏区域边界上的墙.问走出迷宫的最少步数,输出任意一个移动序列. 分 ...

  4. Radar Installation POJ - 1328(贪心)

    Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...

  5. Best Cow Fences POJ - 2018 (二分)

    Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains ...

  6. E - The Balance POJ - 2142 (欧几里德)

    题意:有两种砝码m1, m2和一个物体G,m1的个数x1,  m2的个数为x2, 问令x1+x2最小,并且将天平保持平衡 !输出  x1 和 x2 题解:这是欧几里德拓展的一个应用,欧几里德求不定方程 ...

  7. 人类即将进入互联网梦境时代(IDA)

    在电影<盗梦空间>中,男主角科布和妻子在梦境中生活了50年,从楼宇.商铺.到河流浅滩.一草一木.这两位造梦师用意念建造了属于自己的梦境空间.你或许并不会想到,在不久未来,这看似科幻的情节将 ...

  8. POJ - 2286 - The Rotation Game (IDA*)

    IDA*算法,即迭代加深的A*算法.实际上就是迭代加深+DFS+估价函数 题目传送:The Rotation Game AC代码: #include <map> #include < ...

  9. POJ3460 Booksort(IDA*)

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

随机推荐

  1. Hibernatede 一对多映射配置

    Hibernatede 一对多映射配置 以公司和员工为例:公司是一,员工是多   第一步 创建两个实体类,公司和员工        写核心配置文件hibernate.cfg.xml        写映 ...

  2. 配置一个 Confluence 6 环境

    本部分对你 Confluence 的外部设置进行描述.包括有如何配置 Web 服务器,应用服务器,目录和文件等信息—— Confluence 运行所需要的所有环境.有关在服务器内部对配置进行修改的内容 ...

  3. Confluence 6 避免和清理垃圾

    如果你的 Confluence 是允许公众访问的话,你可能会遇到垃圾内容的骚扰. 阻止垃圾发布者 希望阻止垃圾发布者: 启用验证码(Captcha),请参考页面 Configuring Captcha ...

  4. Confluence 6 布局高级自定义

    重载 Velocity 模板 velocity 目录是 Confluence Velocity 模板文件进行搜索时候需要的文件夹.例如,你可以通过将你的 Velocity 文件使用正确的文件名放置到正 ...

  5. ionic2 子页面隐藏去掉底部tabs导航,子页面全占满显示方法(至今为止发现的最靠谱的方法)

    项目中遇到 tabs 字页面 可以用以下代码隐藏的方式: imports: [ BrowserModule, // IonicModule.forRoot(MyApp), HttpModule, Io ...

  6. 高并发编程基础Synchronized与Volatile

    关键字Synchronized: 当使用Synchrnized (o) ,锁定 o 的时候,锁定的是 o 指向的堆内存中 new 出来的对象,而非 o 引用,当锁定 o 以后,一旦 o 指向了其他对象 ...

  7. Laravel 项目中使用 Bootstrap 框架

    Laravel 如何引入 Bootstrap 如官方文档所言,Laravel 并不强制你使用 CSS 框架,但是开箱提供了对 Bootstrap 的支持,在 resources/js/bootstra ...

  8. bzoj 4715

    其实我并没有见过原题,只是因为...这被改编成了互测题... 题目中提到了一个序列,这个序列是很重要的,否则这个问题好像是没有合理的时间复杂度解法的 但正因为有了这个序列,这个问题的时间复杂度才让人能 ...

  9. Python安装、卸载第三方模块

    pip command ModuleName command:用于指定要执行的命令(install:安装,uninstall:卸载) ModuleName:需要安装的模块名称 示例: 安装第三方模块n ...

  10. Mac下Java JDK的下载安装和配置

    一.下载安装 打开一个搜索引擎,输入JDK,找到Java JDK 如图:  点击打开,同意协议开始下载如图: 下载好以后,安装即可. 安装成功以后,进入根目录,可以找到JDK安装的位置: 资源库——& ...