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. linux /mac 下 go环境变量配置

    安装了go语言之后,还要设置路径,如果不设置路径,则执行 go 的时候会提示 go: command not found,提示的意思是没有这个命令行.这个是因为还没有设置PATH路径. 设置路径的方式 ...

  2. JVM 垃圾回收机制

    首先JVM的内存结构包括五大区域: 程序计数器.虚拟机栈.本地方法栈.方法区.堆区.其中程序计数器.虚拟机栈和本地方法栈3个区域随线程启动与销毁, 因此这几个区域的内存分配和回收都具有确定性,不需要过 ...

  3. Java中的强引用和弱引用

    旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/81114944 一.强引用 如下是强引用的经典 ...

  4. 使用typeof()或者typeof数据类型检测

    使用typeof()或者typeof进行基本数据类型检测(  typeof(X)等于typeof X  返回的是数据类型) 数据类型有:基本数据类型,字符串(string),布尔值(true/fals ...

  5. Jedis 简单案例

    POM 依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> < ...

  6. 手动编译websocket-sharp项目使其支持.net core

    以前项目中使用了websocket-sharp,挺好用.可惜,不支持.net core.好在手动编译很顺利: 从github下载源代码 创建dotnet core的类库,复制代码后并编译即可 dotn ...

  7. java笔记----面试题总结(一)【转】

    1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: - 抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面.抽象只关注对象有哪些属性和行为,并不关注 ...

  8. [20190212]删除tab$记录的恢复3.txt

    [20190212]删除tab$记录的恢复3.txt --//春节前几天做了删除tan$记录的测试,链接:http://blog.itpub.net/267265/viewspace-2565245/ ...

  9. C#面向对象 类的继承

    1.类的访问权限: public:跨程序集,命名空间,必须被using引用: internal:默认当前命名空间: 2.类成员的访问权限: private:私有的,只能当前类: protecte:受保 ...

  10. mssql sqlerver 脚本 计算数据表的结余数的方法分享

    转自:http://www.maomao365.com/?p=5710 摘要:今天接到一个需求,有一张数据表,记录的是消费明细数据,现在需要做一个累计结余,记录每次的数据结余合计,下文将展示一种sql ...