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
f
2, 4, 1, 5, 3, 6
g
, 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
f
3, 4, 5, 1, 2
g
. There are two
ways to do so: cut
f
3, 4, 5
g
and paste after
f
1, 2
g
, or cut
f
1, 2
g
and paste before
f
3, 4, 5
g
.
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 Output
Case 1: 2
Case 2: 1
/**
题目:Editing a Book UVA - 11212
链接:https://vjudge.net/problem/UVA-11212
题意:lrjP208.
思路:启发函数:当前已经操作的步数d,限制的步数maxd,后继不相同的个数x。
由于每一步最多使x减少3,所以如果3*d+x>maxd*3;那么肯定不行。 如果通过位置不同的个数作为启发,是不行的,无法判断。 如果是每个数的后继不同的数有多少个。那么可以推算出每一步操作后,最多减少3个不同的后继。 最终结果所有后继都满足。即a[i] = a[i-1]+1; 具体看lrj算法竞赛入门经典P209; */ #include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int mod=1e9+;
const int maxn=1e2+;
const double eps = 1e-;
int a[], n;
int getDif()///获得后继不合法数.
{
int cnt = ;
for(int i = ; i <= n; i++){
if(a[i]!=a[i-]+){
cnt++;
}
}
return cnt;
}
int temp[];
void Move(int *temp,int *a,int L,int R,int LL,int RR)
{
int now = L;
for(int i = LL; i <= RR; i++){
a[now++] = temp[i];
}
for(int i = L; i <= R; i++){
a[now++] = temp[i];
}
}
bool dfs(int d,int maxd)
{
if(*d+getDif()>*maxd) return false;
if(getDif()==) return true;
int odd[];
memcpy(odd,a,sizeof(int)*(n+));
///如果[L,R]是连续的,那么不需要剪切。
for(int i = ; i <= n; i++){
for(int j = i; j <= n; j++){
for(int k = j+; k <= n; k++){///每次交换[i,j],[j+1,k];每次交换相邻的两段区间。
Move(odd,a,i,j,j+,k);
if(dfs(d+,maxd)) return true;
memcpy(a,odd,sizeof(int)*(n+));///保证每一次操作后,把a复原。
}
}
}
return false;
}
int main()
{
int cas = ;
while(scanf("%d",&n)==)
{
if(n==) break;
for(int i = ; i <= n; i++) scanf("%d",&a[i]);
for(int maxd = ; ; maxd++){
if(dfs(, maxd)){
printf("Case %d: %d\n",cas++,maxd); break;
}
}
}
return ;
}

Editing a Book UVA - 11212 IDA*的更多相关文章

  1. UVA 11212 IDA*

    移动一块连续的区间使得数列递增.问最少次数. 直接IDA*暴搜,只是我没有想到A*函数,所以就随手写了个连续递增块数作为估价函数,WA了,然后除以2,还是WA,除以3,WA,除以4...过了= = # ...

  2. 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 ...

  3. UVA - 11212 Editing a Book (IDA*)

    给你一个长度为n(n<=9)的序列,每次可以将一段连续的子序列剪切到其他地方,问最少多少次操作能将序列变成升序. 本题最大的坑点在于让人很容易想到许多感觉挺正确但实际却不正确的策略来避开一些看似 ...

  4. UVA - 11212 Editing a Book (IDA*搜索)

    题目: 给出n(1<n<10)个数字组成的序列,每次操作可以选取一段连续的区间将这个区间之中的数字放到其他任意位置.问最少经过多少次操作可将序列变为1,2,3……n. 思路: 利用IDA* ...

  5. UVa 11212 Editing a Book (IDA* && 状态空间搜索)

    题意:你有一篇n(2≤n≤9)个自然段组成的文章,希望将它们排列成1,2,…,n.可以用Ctrl+X(剪切)和Ctrl+V(粘贴)快捷键来完成任务.每次可以剪切一段连续的自然段,粘贴时按照顺序粘贴.注 ...

  6. UVA - 11212 Editing a Book(IDA*算法+状态空间搜索)

    题意:通过剪切粘贴操作,将n个自然段组成的文章,排列成1,2,……,n.剪贴板只有一个,问需要完成多少次剪切粘贴操作可以使文章自然段有序排列. 分析: 1.IDA*搜索:maxn是dfs的层数上限,若 ...

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

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

  8. UVA 11212 Editing a Book

    题意: 有一篇由n个自然段组成的文章.希望将他们排成递增序列.只能剪贴和粘贴交替进行,剪贴时可以剪贴一段连续的自然段. 分析: 用IDA*算法求解.当3*d+h>maxd时剪枝. 代码: #in ...

  9. UVa 11212 编辑书稿(dfs+IDA*)

    https://vjudge.net/problem/UVA-11212 题意:给出n个自然段组成的文章,将他们排列成1,2...,n.每次只能剪切一段连续的自然段,粘贴时按照顺序粘贴. 思路:状态空 ...

随机推荐

  1. 如何用JavaScript重定向到另一个网页?

    可以使用 window.location.replace() 方法重定向到另一个页面.相对于 window.location.href ,replace的优势是不会跳转记录并不会保留在历史会话中,这就 ...

  2. ylbtech-LanguageSamples-COMInteropPart1(COM 互操作 - 第一部分)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-COMInteropPart1(COM 互操作 - 第一部分) 1.A,示例(Sampl ...

  3. renderdoc on android

    国内没人发这种贴...一个发了renderdoc with unity是在pc平台跑的 没有挂android 这货有点坑啊 花了好几个小时 wiki上的issue基本全看了...感觉是版本提交的log ...

  4. http://www.oschina.net/question/1019034_153316

    http://www.oschina.net/question/1019034_153316 http://www.oschina.net/question/97503_212116?sort=tim ...

  5. LINUX 和WINDOWS下的自动登录小脚本

    每天上班第一件事,就是连接公司LAB里面的机器,但首先要过一个防火墙,每次输用户名密码是很累人的事, 以下是两个脚本,可以放在启动项中,开机便自动登录 WINDOWS: @echo off ipcon ...

  6. Linux 内核参数 和 Oracle相关参数调整

    Linux 内核参数 和 Oracle相关参数调整 分类: Oracle Basic Knowledge2009-10-14 12:23 9648人阅读 评论(0) 收藏 举报 oraclelinux ...

  7. Server 非阻塞

    import socket import select import Queue port =500 host = "" sock = socket.socket(socket.A ...

  8. unity web项目发布服务器Data file is corrupt (not a Unity W

    楼上问题需要在iis 中配置MIME 加一个 .unity3d MIME类型:application/octet-stream http://www.cnblogs.com/123ing/p/3913 ...

  9. mui 跨域请求

    <ul class="mui-table-view" style="margin-top: 25px;"> <li class="m ...

  10. 《深入理解jvm》笔记---第七章

    虚拟机类载入机制 1. 类的生命周期: 载入.验证.准备.解析.初始化.使用.卸载七个阶段.当中验证.准备.解析三个阶段统称为连接. 当中,解析的阶段的时机并不一定. 2. Java类载入的时机: J ...