Problem UVA11212-Editing a Book

Accept:572  Submit:4428

Time Limit: 10000 mSec

 Problem Description

You have n equal-length paragraphs numbered 1 to n. Now you want to arrange them in the order of 1,2,...,n. With the help of a clipboard, you can easily do this: Ctrl-X (cut) and Ctrl-V (paste) several times. You cannot cut twice before pasting, but you can cut several contiguous paragraphs at the same time - they’ll be pasted in order. For example, in order to make {2, 4, 1, 5, 3, 6}, you can cut 1 and paste before 2, then cut 3 and paste before 4. As another example, one copy and paste is enough for {3, 4, 5, 1, 2}. There are two ways to do so: cut {3, 4, 5} and paste after {1, 2}, or cut {1, 2} and paste before {3, 4, 5}.

 Input

The input consists of at most 20 test cases. Each case begins with a line containing a single integer n (1 < n < 10), thenumber of paragraphs. The next line contains a permutation of 1,2,3,...,n. The last case is followed by a single zero, which should not be processed.

 Output

For each test case, print the case number and the minimal number of cut/paste operations.

 Sample Input

6
2 4 1 5 3 6
5
3 4 5 1 2
0
 

 Sample Ouput

Case 1: 2

Case 2: 1

题解:第一到IDA*算法的题目。迭代加深搜索,就是在普通DFS上加了个深度限制,如果目前的深度无法得到结果,就让深度限制变大,直到找到解。该算法中最重要的就是估价函数,用该函数进行剪枝操作,当前深度为d,如果最理想的情况下还要搜索h层,那么当d+h > maxd时显然就可以剪枝,其余部分和普通dfs区别不大。

续:今天对这道题又进行了一个小小的尝试,收获很大。之所以对它进行二次尝试,主要是因为第一次写时按照lrj的思路copy的,想真正自己实现一次,这次实现完全以lrj在讲解该算法时的思路为框架进行code,dfs第一步判断深度是否达到maxd,达到了就进行判断是否成立,返回信息。关键在于下一步的是先枚举还是先剪枝,如果先剪枝,两个代码的效率几乎没有差距,如果先枚举,直接TLE,这份代码用时640ms而限制是10000ms,这一个顺序使得效率差了十倍不止,仔细想了想,先剪枝与后剪枝相比,把剪枝操作提前了一层,对于每一层都有很多节点的搜索来说是很不划算的。千万注意这个顺序!!!

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; const int maxn = ;
int n,order[maxn];
int maxd; int cal() {
int cnt = ;
for (int i = ; i < n-; i++) {
if (order[i] != order[i + ] - ) cnt++;
}
if (order[n - ] != n) cnt++;
return cnt;
} bool is_sorted() {
for (int i = ; i < n - ; i++) {
if (order[i] >= order[i + ]) return false;
}
return true;
} bool dfs(int d) {
if ( * d + cal() > maxd * ) return false;
if (is_sorted()) return true; int old[maxn],now[maxn];
memcpy(old, order, sizeof(order));
for (int i = ; i < n; i++) {
for (int j = i; j < n; j++) {
int cnt = ;
for (int k = ; k < n; k++) {
if (k < i || k > j) {
now[cnt++] = order[k];
}
}
for (int k = ; k <= cnt; k++) {
int cnt2 = ;
for (int p = ; p < k; p++) order[cnt2++] = now[p];
for (int p = i; p <= j; p++) order[cnt2++] = old[p];
for (int p = k; p < cnt; p++) order[cnt2++] = now[p];
if (dfs(d + )) return true;
memcpy(order, old, sizeof(order));
}
}
}
return false;
} int iCase = ; int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d", &n) && n) {
for (int i = ; i < n; i++) {
scanf("%d", &order[i]);
}
for (maxd = ; maxd < n; maxd++) {
if (dfs()) break;
}
printf("Case %d: %d\n", iCase++, maxd);
}
return ;
}

UVA11212-Editing a Book(迭代加深搜索)的更多相关文章

  1. UVA 11212 Editing a Book [迭代加深搜索IDA*]

    11212 Editing a Book You have n equal-length paragraphs numbered 1 to n. Now you want to arrange the ...

  2. uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索

    迭代加深搜索 自己看的时候第一遍更本就看不懂..是非常水,但智商捉急也是没有办法的事情. 好在有几个同学已经是做过了这道题而且对迭代加深搜索的思路有了一定的了解,所以在某些不理解的地方询问了一下他们的 ...

  3. 7-10Editing aBook uva11212(迭代加深搜索 IDA*)

    题意:  给出n( 2<=n<=9) 个乱序的数组  要求拍成升序  每次 剪切一段加上粘贴一段算一次  拍成1 2 3 4 ...n即可     求排序次数 典型的状态空间搜索问题   ...

  4. POJ1129Channel Allocation[迭代加深搜索 四色定理]

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

  5. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  6. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  7. 迭代加深搜索 codevs 2541 幂运算

    codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

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

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

  9. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  10. hdu 1560 DNA sequence(迭代加深搜索)

    DNA sequence Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

随机推荐

  1. Java编程思想__异常

    1.使用异常链,需要采用如下方式包装捕获到的异常: public void two() { System.out.println("two()"); try { one(); } ...

  2. 16-pymysql模块的使用

    [转]16-pymysql模块的使用 本节重点: pymysql的下载和使用 execute()之sql注入 增.删.改:conn.commit() 查:fetchone.fetchmany.fetc ...

  3. Python中字典dict

    dict字典 字典是一种组合数据,没有顺序的组合数据,数据以键值对形式出现 # 字典的创建 # 创建空字典1 d = {} print(d) # 创建空字典2 d = dict() print(d) ...

  4. ios12怎么投屏电脑 苹果手机怎么投

    Ios12系统发布成功之后,是不是给我们带来更大的惊喜呢.我们只需要利用手机上的屏幕镜像就可以轻松将手机画面投屏至电脑上,那么ios12怎么投屏电脑?下面便是今天所要分享的手机投屏的方法. 使用工具: ...

  5. [ORACLE]ORA-28002 The password will expire within 7 days.将不能登录系统

    错误“ORA-28002 The password will expire within 7 days.  Cannot logon to the database“当在进程调度器上运行AE程序可能遇 ...

  6. Javascript 对象 - 数组对象

    JavaScript核心对象 数组对象Array 字符串对象String 日期对象Date 数学对象Math 数组对象 数组对象是用来在单一的变量名中存储一系列的值.数组是在编程语言中经常使用的一种数 ...

  7. java 返回某一天的周日和现在这一周的周日

    import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import j ...

  8. apache php mysql 安装

    推荐参考这里:http://www.myhack58.com/Article/sort099/sort0100/2012/35578_3.htm

  9. php get接口,并在浏览器中以json格式返回查找到的数据

    php查询数据有6个步骤,分别为: 连接数据库服务器,使用的命令为:mysql_connect("服务器名称","用户名","密码") 选择 ...

  10. SQL Server遗失管理权限账号密码怎么办?

    假如一个SQL Server实例只允许"SQL身份认证"模式登录数据库,而糟糕的是你忘记了sa的密码(sa出于安全考虑应该被禁用,这里仅仅为了描述问题)或其它具有sysadmin角 ...