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
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,这一个顺序使得效率差了十倍不止,仔细想了想,先剪枝与后剪枝相比,把剪枝操作提前了一层,对于每一层都有很多节点的搜索来说是很不划算的。千万注意这个顺序!!!
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 7 using namespace std; 8 9 const int maxn = 9; 10 int n,order[maxn]; 11 int maxd; 12 13 int cal() { 14 int cnt = 0; 15 for (int i = 0; i < n-1; i++) { 16 if (order[i] != order[i + 1] - 1) cnt++; 17 } 18 if (order[n - 1] != n) cnt++; 19 return cnt; 20 } 21 22 bool is_sorted() { 23 for (int i = 0; i < n - 1; i++) { 24 if (order[i] >= order[i + 1]) return false; 25 } 26 return true; 27 } 28 29 bool dfs(int d) { 30 if (3 * d + cal() > maxd * 3) return false; 31 if (is_sorted()) return true; 32 33 int old[maxn],now[maxn]; 34 memcpy(old, order, sizeof(order)); 35 for (int i = 0; i < n; i++) { 36 for (int j = i; j < n; j++) { 37 int cnt = 0; 38 for (int k = 0; k < n; k++) { 39 if (k < i || k > j) { 40 now[cnt++] = order[k]; 41 } 42 } 43 for (int k = 0; k <= cnt; k++) { 44 int cnt2 = 0; 45 for (int p = 0; p < k; p++) order[cnt2++] = now[p]; 46 for (int p = i; p <= j; p++) order[cnt2++] = old[p]; 47 for (int p = k; p < cnt; p++) order[cnt2++] = now[p]; 48 if (dfs(d + 1)) return true; 49 memcpy(order, old, sizeof(order)); 50 } 51 } 52 } 53 return false; 54 } 55 56 int iCase = 1; 57 58 int main() 59 { 60 //freopen("input.txt", "r", stdin); 61 while (~scanf("%d", &n) && n) { 62 for (int i = 0; i < n; i++) { 63 scanf("%d", &order[i]); 64 } 65 for (maxd = 0; maxd < n; maxd++) { 66 if (dfs(0)) break; 67 } 68 printf("Case %d: %d ", iCase++, maxd); 69 } 70 return 0; 71 }