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. MySQL查询时区分大小写(转)

    说明:在MySQL查询时要区分大小写会涉及到两个概念character set和collation,这两个概念在表设计时或者在查询时都可以指定的,详细参考:http://www.cnblogs.com ...

  2. Nice Messager隐私权政策

    重视用户的隐私.您在使用我们的服务时,我们可能会收集和使用您的相关信息.我们希望通过本<隐私政策>向您说明,在使用我们的服务时,我们如何收集.使用.储存和分享这些信息,以及我们为您提供的访 ...

  3. mysql left join,right join,inner join的区别

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录 inner join(等值连接) ...

  4. 刷新神经网络新深度:ImageNet计算机视觉挑战赛微软中国研究员夺冠

    微软亚洲研究院首席研究员孙剑 世界上最好计算机视觉系统有多精确?就在美国东部时间12月10日上午9时,ImageNet计算机视觉识别挑战赛结果揭晓——微软亚洲研究院视觉计算组的研究员们凭借深层神经网络 ...

  5. ubuntu中apt使用以及centos中yum的使用

    centos和ubuntu是两大linux主流阵营 在centos中下载安装软件的方式 rpm rpm命令是RPM软件包的管理工具.rpm原本是Red Hat Linux发行版专门用来管理Linux各 ...

  6. Python数据整合与数据准备-BigGorilla实例应用

    参考文档:http://www.biggorilla.org/walkt/ 一.BigGorilla应用主要步骤 如下图: 二.实例应用 1.数据获取 urllib是非常受欢迎的用于在网络上读取数据的 ...

  7. Linux源码编译安装MySQL5.7

    目录[-] 一.环境准备: 二.升级系统: 三.做一些准备工作(以下Linux命令均在su到root用户操作): 四.开始编译安装mysql-5.7.9: 一.环境准备: 我尝试过以下环境都是能成功的 ...

  8. 【Git】Git hangs while unpacking objects (Windows)

    Git hangs while unpacking objects (Windows) 14 Oct 2014 I'm not sure if this is because we're behind ...

  9. Netty4 ServerBootstrap 初始化channelFactory ReflectiveChannelFactory

    只需要在启动之前传入你需要用的channel类型就可以了. ServerBootstrap初始化channelFactory过程: 最后我们再来看看这个channelFactory的使用场景:

  10. 安装dubbo-admin报错 URIType BeanCreationException

    安装dubbo-admin报错 URIType BeanCreationException 学习了:https://blog.csdn.net/lsm135/article/details/52725 ...