飞来山上千寻塔,闻说鸡鸣见日升。

不畏浮云遮望眼,自缘身在最高层。——王安石

题目:Booksort

网址:http://poj.org/problem?id=3460

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.

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

首先,理解:选取某一段剪切至另一位置的后面,等价于将该段到该位置的区间到该段前面;

若选区间[L, R)将其剪切至pos的后面(pos >= R),等价于将[R, pos ]剪切至L之前。

故只考虑将一区间剪切至后面的位置即可,避免重复计算相同状态了。

看起来整一棵搜索树的规模是无穷无尽的,因此即便限制深度为5,效率并不高;

可以考虑以下做法:

  • 双向广搜

    不解释;

  • IDA*

    估价函数为错误后继的个数tot除以三;

    若[tot / 3] + now.dep > dep,剪枝;

    不解释。

代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector> using namespace std; int n, p[16];
bool valid(int *s)
{
for(int i = 1; i <= n; ++ i) if(s[i - 1] != i) return false;
return true;
}
void flip(int *s, int cur, int len, int *c, int pos)
{
vector <int> q;
q.clear();
for(int i = 0; i < cur; ++ i) q.push_back(c[i]); for(int i = cur + len; i <= pos; ++ i) q.push_back(c[i]);
for(int i = 0; i < len; ++ i) q.push_back(s[i]);
for(int i = pos + 1; i < n; ++ i) q.push_back(c[i]); for(int i = 0; i < n; ++ i) c[i] = q[i];
return;
}
bool dfs(int *a, int dep)
{
if(valid(a)) return true;
if(dep == 0) return false;
int num = 0;
for(int i = 0; i < n - 1; ++ i) if(a[i + 1] != a[i] + 1) ++ num;
if(num > dep * 3) return false;
int copy[16], s[15];
memcpy(copy, a, sizeof(copy));
for(int len = 1; len < n; ++ len)//限制了长度
{
for(int i = 0; i + len < n; ++ i)//定位
{
for(int k = i; k < i + len; ++ k) s[k - i] = a[k];
for(int j = i + len; j < n; ++ j)
{
flip(s, i, len, copy, j);//复制粘贴函数
if(dfs(copy, dep - 1)) return true;
memcpy(copy, a, sizeof(copy));
}
}
}
return false;
}
int main()
{
int T, copy[16];
int dep;
scanf("%d", &T);
bool ok = false;
while(T --)
{
scanf("%d", &n);
memset(p, 0, sizeof(p));
for(int i = 0; i < n; ++ i) scanf("%d", &p[i]);
for(dep = 0; dep < 5; ++ dep)
{
memcpy(copy, p, sizeof(copy));
if(dfs(copy, dep))
{
printf("%d\n", dep);
ok = true;
break;
}
}
if(ok) ok = false;
else puts("5 or more");
}
return 0;
}

最新文章

  1. java多线程详解(7)-线程池的使用
  2. 从一个QQ群友那儿偷来的js图形 ^_^
  3. jetty服务器的安装和部署、新增到开机启动服务
  4. JavaScript数据结构——栈和队列
  5. 浅析JNI函数的注册过程
  6. python学习第二天第一部分
  7. 【转载】Javascript中的this关键字
  8. CCS5 建立SYS/BIOS工程时报错“cannot find file &quot;./configPkg/linker.cmd&quot; bios”的解决方法
  9. 标签form表单里的属性简单介绍了一下
  10. 黑马程序员_&lt;&lt;GUI(图形用户界面)--------1&gt;&gt;
  11. noip 2009 细胞分裂
  12. 使用isql连接Sybase ASE数据库的常见错误及处理方式
  13. 解决https无法缓存的问题
  14. Anaconda 安装概要
  15. Auto Create Editable Copy Font(Unity3D开发之二十二)
  16. 背水一战 Windows 10 (116) - 后台任务: 前台程序激活后台任务
  17. P2590 [ZJOI2008]树的统计
  18. 【python】threadpool的内存占用问题
  19. nuxtjs中使用axios
  20. python入门 -- 学习笔记1

热门文章

  1. python数据库MySQL之视图,触发器,事务,存储过程,函数
  2. Oracle创建函数例子
  3. 1012 The Best Rank (25 分)
  4. 邮件服务TLS/SSL,CA证书
  5. Linux服务器架设篇,DNS服务器(二),cache-only DNS服务器的搭建
  6. 小程序后台隐藏后 socket 无法重新连接
  7. std::string构造函数
  8. 一个hql 关键字member(非mysql)引起的 vo 数据 保存数据库错误
  9. ES6构造函数class 和 ES5构造函数语法
  10. AJ学IOS 之小知识之xcode6自动提示图片插件 KSImageNamed的安装