POJ3460 Booksort
飞来山上千寻塔,闻说鸡鸣见日升。
不畏浮云遮望眼,自缘身在最高层。——王安石
题目: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;
}
POJ3460 Booksort的更多相关文章
- POJ3460 Booksort(IDA*)
POJ3460 Booksort 题意:给定一个长度为n的序列,每次可以取出其中的一段数,插入任意一个位置,问最少需要几次操作才能使整个序列变为1~n 思路:IDA*+迭代加深搜索 小技巧:将一段数插 ...
- A*专题训练
POJ2449 Remmarguts' Date UDF's capital consists of N stations. The hall is numbered S, while the sta ...
- hdu 1685 Booksort (IDA*)
Booksort Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) Tot ...
- POJ 3460 Booksort(算竞进阶习题)
IDA* 这题真不会写..估价函数太巧妙了.. 按照lyd神牛的说法我们把a[i+1]=a[i]+1记为正确后继,反之则记为错误后继 那么考虑最优的一次交换区间,至多能够纠正三个错误后继,所以我们统计 ...
- Booksort POJ - 3460 (IDA*)
Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...
- 【POJ 3460】 Booksort
[题目链接] http://poj.org/problem?id=3460 [算法] IDA* 注意特判答案为0的情况 [代码] #include <algorithm> #include ...
- acm位运算应用 搜索
acm位运算应用 搜索 搜索 此处不讲题目,只讲位运算是怎样在这些题中实现和应用的.由于搜索题往往是基于对状态的操作,位运算往往特别有效,优化之后的效果可以有目共睹. 例1.POJ 132 ...
- Server Tomcat v7.0 Server at localhost failed to start.
1:这里记录一下这个错误,反正百度一大推,但是很长很长,我感觉这个问题肯定是servlet引起的,因为我遇到的总是如此: 2:我的问题如下所示: <servlet> <servlet ...
- scrapy爬去京东书籍信息
# -*- coding: utf-8 -*- import scrapy import urllib import json from copy import deepcopy class JdSp ...
随机推荐
- 老技术新谈,Java应用监控利器JMX(1)
先聊聊最近比较流行的梗,来一次灵魂八问. 配钥匙师傅: 你配吗? 食堂阿姨: 你要饭吗? 算命先生: 你算什么东西? 快递小哥: 你是什么东西? 上海垃圾分拣阿姨: 你是什么垃圾? 滴滴司机: 你搞清 ...
- Nagios监控服务
Nagios监控服务 案例1:常用系统监控命令 案例2:搭建nagios监控服务器 案例3:配置文件及插件使用 案例4:监控远程主机的公有数据 案例5:监控远程主机的私有数据 1 案例1:常用系统监控 ...
- Vulnhub DC-6靶机渗透
信息搜集 nmap -sP 192.168.146.0/24 #找靶机ip nmap -sS -Pn -A 192.168.146.143 #扫描靶机信息 22和80端口,老朋友了. 先直接访问htt ...
- 第一章 AT&T
1.一个公司(企业)越庞大,就越危险:越复杂,就越濒临坍塌:快速发展的同时,也埋下了隐患. 2.再庞大的企业也不可能永久站立,下个十年谁也说不准谁会在浪潮之巅. 3.一个人能走多远,往往取决于他能看多 ...
- Powershell检查邮件队列设置阈值,通过html形式进行邮件告警
为了完善公司的整体邮件质量,博主通过zabbix监控了exchange的所有微软推荐项目,并写了很多powershell来辅佐, 旨在更大程度上提高整体的邮件性能 这篇文章主要是讲通过powershe ...
- ln -s 软链接命令
所有对软链接link_name的操作都是对目录或文件dir_file的操作 ln -s [dir_file] [link_name]
- AJ学IOS(46)之网易彩票幸运大转盘
AJ分享,必须精品 效果 实现过程: 基础UI搭建 这里主要是用了xib搭建,首先我们分析,有中间的开始按钮,背景图片,还有星座按钮,这里能用xib做的事开始按钮和背景图片. 如图: 星座按钮的搭建: ...
- 【Tool】在Windows系统上,下载和安装当前最新版本的IDEA 2020-4-14
下载 & 安装 IDEA 下载部分: 官网地址:https://www.jetbrains.com/idea/ 直接点击鲜眼的DOWNLOAD 如果仅仅是想简单接触学习下Java语言,社区版的 ...
- delphi中DateTimePicker控件同时输入日期和时间
将DateTimePicker的Format属性中加入日期格式设成 'yyyy-MM-dd HH:mm',注意大小写 , 将kind设置为dtkTime即可,可以在每次Form onShow时将Dat ...
- 这价格看得我偷偷摸了泪——用python爬取北京二手房数据
如果想了解更多关于python的应用,可以私信我,或者加群,里面到资料都是免费的 http://t.cn/A6Zvjdun 近期,有个朋友联系我,想统计一下北京二手房的相关的数据,而自己用Excel统 ...