Booksort 启发式函数很重要h(s1)<=h(s2)+cost(s1,s2);

2023-05-16

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
***************************************************************************************************************************
***************************************************************************************************************************

  1 /*
  2 其中启发式函数要满足三角不等式h(s1)<=h(s2)+cost(s1,s2);
  3 
  4 */
  5 #include<iostream>
  6 #include<string>
  7 #include<cstring>
  8 #include<cstdio>
  9 #include<cmath>
 10 using namespace std;
 11 int n,book[30];
 12 //int i,j,k;
 13 bool flag;
 14 int lim;
 15 bool isok()
 16 {
 17   int i,j,k;
 18   for(i=1;i<=n;i++)
 19    if(book[i]!=i)
 20     return false;
 21   return true;
 22 }
 23 int h()//启发式函数
 24 {
 25     int ret=0;
 26     int i;
 27     if(book[1]!=1)
 28       ret++;
 29     if(book[n]!=n)
 30       ret++;
 31     for(i=2;i<n;i++)
 32      if(book[i]+1!=book[i+1])
 33         ret++;
 34     return ret;
 35 }
 36 void put(int st,int len,int pos)//数组的移动
 37 {
 38     int tmp[30];
 39     int it,jt;
 40     for(it=1;it<=len;it++)
 41      tmp[it]=book[st+it-1];
 42     if(pos>st)
 43     {
 44       for(it=st+len;it<=pos;it++)
 45         book[it-len]=book[it];
 46       for(it-=len,jt=1;it<=pos;it++,jt++)
 47         book[it]=tmp[jt];
 48     }
 49     else
 50     {
 51         for(it=st-1;it>pos;it--)
 52          book[it+len]=book[it];
 53         for(it=1;it<=len;it++)
 54          book[it+pos]=tmp[it];
 55     }
 56 }
 57 void dfs(int dp)
 58 {
 59     if(flag)
 60        return;
 61     if(dp*3+h()>lim*3)//如果不满足条件,则剪枝返回
 62       return;
 63     if(isok())
 64     {
 65         flag=true;
 66         return;
 67     }
 68     int i,j,k;
 69     int tmp[30];
 70     for(i=1;i<=n;i++)
 71      tmp[i]=book[i];
 72     for(i=1;i<n;i++)
 73     {
 74         for(j=1;i+j<=n+1;j++)
 75         {
 76             for(k=0;k<=n;k++)
 77             {
 78                 if(j<=k&&i+j<k)
 79                  continue;
 80                 put(j,i,k);
 81                 dfs(dp+1);
 82                 for(int tt=1;tt<=n;tt++)//还原
 83                  book[tt]=tmp[tt];
 84             }
 85         }
 86     }
 87 
 88 }
 89 int main()
 90 {
 91   int i,j,k,cas;
 92   scanf("%d",&cas);
 93   while(cas--)
 94   {
 95       scanf("%d",&n);
 96       lim=0;
 97       for(i=1;i<=n;i++)
 98        {
 99            scanf("%d",&book[i]);
100            if(book[i]==i)
101             lim++;
102        }
103        if(lim==n)
104        {
105            printf("0\n");
106            continue;
107        }
108        lim=1;
109        flag=false;
110        while(1)
111        {
112            dfs(0);
113            if(flag)
114             break;
115            lim++;
116            if(lim>4||flag)
117              break;
118 
119        }
120        if(flag)
121         printf("%d\n",lim);
122        else
123         printf("5 or more\n");
124     }
125     return 0;
126 }  
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3438037.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Booksort 启发式函数很重要h(s1)<=h(s2)+cost(s1,s2); 的相关文章

  • UML实例(五):在线购物系统设计类图

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 在线购物系统设计类图文档 xff1a 1 图形文档 设计类图 界面类图 2 文字说明 该部分由以下部分组成 xff1a 类图综述 类描述 类联描述 继承描述 依赖描述和其他
  • 为啥很多网站的用户端都更新了,博客园还是原来的样子呢

    重回博客园发布一些工作中的文章 xff0c 心得 xff0c 发现博客园在界面上还是跟7 8年前没啥区别 xff0c 界面经典好用 xff0c 这是不用质疑的 xff0c 但是发布内容的一些地方 xff0c 好像不像其他网站那么方便了 xf
  • ASP.NET的必须知道的东东(HttpModule,HttpHandler)

    asp net架构 一 asp net请求的处理过程 xff0d xff0d xff0d xff0d xff0d xff0d xff0d xff0d xff0d xff0d xff0d xff0d xff0d xff0d xff0d xff
  • matlab练习程序(Hilbert图像置乱)

    正好刚写了Hibert生成曲线 xff0c 不如再加一篇应用的程序 关于Hilbert图像置乱 xff0c 我在网上搜的应用领域主要集中在数字水印和图像加密上 xff0c 而这两个领域我都没怎么接触过 大部分的图像置乱都是如下图的置乱1所示
  • 深入C++“准”标准库,Boost你的力量

    最近一年我电话面试了数十位 C 43 43 应聘者 xff0c 惯用的暖场问题是 工作中使用过 STL 的哪些组件 xff1f 使用过 Boost 的哪些组件 xff1f 得到的答案大多集中在 vector map 和 shared ptr
  • Visual Studio 中创建带有向导的项目模板

    对于测试开发来说 xff0c 建立新工程的次数要远远高于专职开发人员 由于每次建立一个测试工程都要例行公事的设置一大堆属性 xff0c 例行公事的写一些同样的代码 xff0c 非常耗时 因此打算通过建立项目模板来达到自己完成的目的 比如 x
  • Linux -- Samba之SWAT(Web服务器和CGI脚本应用程序)

    6 6 2 SWAT xff08 1 xff09 SWAT xff08 Samba Web Administration Tool xff0c Samba Web 管理工具 xff09 是一个小规模的Web服务器和CGI脚本应用程序 可以为
  • 关于openstack里instance的mac地址

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 假设你在虚拟机里面的使用ifconfig看到的它自己的mac地址为A xff0c 然后在其他随便一台机器上ssh连接一下这个虚拟机 xff08 连接它的floating i
  • 关于linux终端表述,Linux 终端tty pty pts描述

    在使用Linux的过程中 xff0c 当我们通过ssh或者telnet等方式连接到服务器之后 xff0c 会有一个相应的终端来对应 而在直接登陆到Linux服务器的时候也有一个对应的终端 也就是说所有登陆到当前Linux服务器的用户都有一个
  • 指定的网络名不再可用的解决方法

    2009 06 04 16 40 自己用的一台服务器总是出现 34 指定的网络名不再可用 34 这个问题 用ip来访问总是不成功 在网上找了许多方法也不行后来终于找到解决方法了 原因是两个服务没有启动 第一个是Computer Brower
  • Ubuntu使用VNC运行基于Docker的桌面系统

    2019独角兽企业重金招聘Python工程师标准 gt gt gt docker ubuntu vnc desktop From Docker Index docker pull dorowu ubuntu desktop lxde vnc
  • vncserver Can&#39;t find file /root/.vnc/192.168.1.3:1.pid You&#39;ll have to kill the Xvnc process ...

    CentOS 6 5 修改IP后VNC链接失败 xff0c 提示 xff1a The connection was refused by host computer 尝试删除之前的服务时提示以下信息 xff1a Can 39 t find
  • 国内首个网友可以开发应用的开放式网络操作系统

    国内首个面向电脑爱好者的应用开放平台 X在线电脑 xff0c 提供傻瓜化的网络应用程序开发工具 应用梦工厂 xff0c 让不擅长编程的电脑爱好者们 xff0c 也能开发OA ERP等企业应用 xff0c 并且能快速部署到X在线电脑 X在线电
  • linux多级菜单脚本教程,Linux下使用readline库编程实现多级CLI菜单

    一 背景 CLI是一种快速简洁的人机交互方式 xff0c 优秀的CLI 如 mysql vtysh gdb 带给我们非常好的体验 那么CLI都是如何开发出来的 xff1f 二 相关知识 2 1 CLI vs GUI 文章 1 纵观CLI与G
  • 白盒交换机NOS列表(picos/SnapRoute/ONL)

    WIKI NOS xff1a https en wikipedia org wiki Network operating system Examples JUNOS used in routers and switches from Jun
  • powershell

    常用单行命令 目录 查看当前目录的大小 xff0c 并排序输出 du h max depth 61 1 sort nr 自动选择单位 du m max depth 61 1 sort nr 选择M为单位 转载于 https www cnbl
  • 私有云对企业来说有什么好处

    企业是一个受控集团 xff0c 只有良好的管理 决策 xff0c 一个企业才有成功的希望 xff0c 所以管理在企业中占有重要的地位 私有云的使用是一只无形的手 xff0c 它控制着日常工作中的资源和效率 1 企业拥有基础设施 xff0c
  • JavaScript禁用页面刷新

    JavaScript禁用页面刷新代码如下 xff1a 禁用F5刷新 document onkeydown 61 function if event keyCode 61 61 116 event keyCode 61 0 event can
  • java 整除(/) 求余(%) 运算

    1 java 整除 求余 运算 1 求余 System out println 11 2 顾名思义就是11除2的余数 gt 1 System out println 11 2 结果 gt 1 System out println 11 2
  • C# 解决窗体闪烁

    C 解决窗体闪烁 在Windows窗体上造成 闪烁 的窗体上有很多控制 造成这种闪烁的原因有两个 xff1a 1 当控件需要被绘制时 xff0c Windows发送一个控件两个消息 第一个 xff08 WM ERASEBKGND xff09

随机推荐

  • OVN – OVN OpenStack(二)

    OpenStack networking ovn 项目为Neutron提供了一个基于ML2的OVN插件 xff0c 它使用OVN组件代替了各种Neutron的Python agent xff0c 也不再使用 RabbitMQ xff0c 而
  • 飞秋无法显示局域网好友

    1 飞秋无法显示局域网好友 无法查看网上邻居 无法适用共享打印机的问题是由于开启了 局域网隐身 的缘故 xff0c 打开 360安全卫士 xff1e 功能大全 xff1e 网络优化 xff1e 流量防火墙 xff1e 局域网防护 xff0c
  • pandas 按照某一列进行排序

    pandas排序的方法有很多 xff0c sort values表示根据某一列排序 pd sort values 34 xxx 34 inplace 61 True 表示pd按照xxx这个字段排序 xff0c inplace默认为False
  • 关系数据库和NoSQL结合使用:MySQL + MongoDB

    Home Page 作者使用一个案例来说明MySQL 43 MongoDB结合使用 xff0c 发挥各自所长 xff0c 并且认为他们互补性很强 当然 xff0c 这其中不可避免引入DDD中的编程设计模式 Repository仓储模式 xf
  • 查看网卡信息及状态和网卡日志信息

    查看网卡信息 1 mii tool v w em1 em2 l0 em1 negotiated 100baseTx FD link ok product info vendor 00 aa 00 model 57 rev 1 basic m
  • 筛选出sql 查询结果中 不包含某个字符

    select from table1 where patindex 39 关键字 39 aa 61 0 select from table1 where charindex 39 关键字 39 aa 61 0 select from tab
  • IE8正式版引发VS2005和VS2008添加变量向导出错的解决方案

    1 解决办法1 xff1a 2 卸载IE8 3 解决办法2 xff1a xff08 自己使用的方法 xff09 4 5 打开注册表编辑器 6 7 选择 HKEY CURRENT USER Software Microsoft Windows
  • Visual Studio 2010中文旗舰版+大家所关心的

    下载地址 xff08 VS2010不含MSDN xff09 xff1a http download microsoft com download 2 4 7 24733615 AA11 42E9 8883 E28CDCA88ED5 X16
  • CSS列表

    CSS列表属性可以放置 改变列表项的标志 xff0c 或者将图像作为列表项标志 list style xff1a 简写属性 用于把所有用于列表的属性设置在一个声明中 list style image xff1a 将图像设置为列表项的标志 U
  • Lodash源码讲解-compact函数

    原文首发于Lodash源码讲解 这是我们阅读Lodash源码的第3篇博客 xff0c 在这篇文章里我们来学习一下Lodash的compact方法 compact函数内部没有依赖别的函数 xff0c 让我们先来看一下compact函数的源码
  • CentOS 6.5下Squid代理服务器的安装与配置

    一 系统环境 操作系统 xff1a CentOS release 6 5 Squid版本 xff1a squid 3 1 10 20 el6 5 3 x86 64 SELINUX 61 disabled HTTP Service stope
  • 修改VNCSERVER的分辨率

    使用VNC远程连接时 xff0c 最大化窗口后仍旧在中间显示一个小屏幕 xff0c 并没有随着窗口最大化 xff0c 解决该问题需要首先在VNC窗口标题栏右键 gt Options gt Scaling 选择第二项 xff1a Scale
  • XMLHttpRequest - 原始AJAX初步

    我们知道 xff0c 传统的Web应用是request response形式的 xff0c 即浏览器向服务器发送请求 xff0c 服务器进行处理 xff0c 然后再对浏览器响应 这种形式最大的缺点就是 xff1a 客户端需要等服务器处理完之
  • Python面向对象编程 - 一个记事本程序范例(二)

    给程序加上控制台菜单 menu py import sys from notebook import Notebook Note class Menu 39 39 39 Display a menu and respond to choic
  • 个人代码库の自动粘合桌面边缘

    using System Windows Forms using System namespace public partial class form 必要事件 xff1a No 1 xff1a 窗体的 Move 事件 No 2 xff1a
  • 完全参照系统自带的DatePickerDialog、TimePickerDialog的源代码仿写的DateTimePickerDialog...

    完全参照系统自带的DatePickerDialog TimePickerDialog的源代码仿写的DateTimePickerDialog 具有同时选择日期 时间的功能 在2 2 2 3平台 xff0c 显示的效果可能会有一个大背景框在后面
  • Tracking your habits in Org-mode

    纯属记录 在org mode中 xff0c 你可以跟踪你的周期性事务或辅助培养习惯 xff0c 比如每天阅读半小时 xff0c 每天完成后org mode会予以记录 如果你正计划每月培养一个好习惯 xff0c 也可以使用这个功能来记录你的完
  • 算法的力量

    算法的力量 李开复 真正学懂计算机的人 xff08 不只是 编程匠 xff09 都对数学有相当的造诣 xff0c 既能用科学家的严谨思维来求证 xff0c 也能用工程师的务实手段来解决问题 而这种思维和手段的最佳演绎就是 算法 虽然在摩尔定
  • Xstream序列化实体

    Java序列化的日期为是很标准 xff0c XStream中转换为标准的做法 import java text DateFormat import java text ParseException import java text Simp
  • Booksort 启发式函数很重要h(s1)<=h(s2)+cost(s1,s2);

    Problem Description The Leiden University Library has millions of books When a student wants to borrow a certain book he