题意

你有一篇由n(2<=n<=9)个自然段组成的文章,希望将它们排列成1,2,···,n。可以用剪切和粘贴来完成任务。每次可以剪切一段连续的自然段,粘贴时按照顺序粘贴。注意剪贴板只有一个,所以不能连续剪切两次,只能剪切和粘贴交替。

分析

鄙人真的不擅长搜索,花了两天时间做完搜索专题的作业,然后被这一道题卡了一天,结果今下午的比赛又是搜索专题,我又不知道得补到啥时候···难受!

这个题是比较显然的IDA*,主要是估值函数h()的计算。如果当前有res个数字的位置不正确,而每次剪切h()最多可以减少3.所以当h()>cur的时候,return false。

然后还加了一个优化(不影响ac,但是会快不少),就是如果存在连续的一段,在cut的时候不把他们拆开。

下面是ac的代码

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std;
const int maxn=;
int a[maxn],b[maxn];
int n,kase;
bool judge(){
for(int i=;i<=n;i++){
if(b[i]!=i)
return false;
}
return true;
}
void ope(int from,int len,int to){//将从from开始,长度为len的一段序列,复制到to位置,然后to位置到from-1位置原来的元素后延
int c[maxn];
for(int i=from,j=;j<=len;i++,j++)
c[j]=b[i];
if(from>to){
for(int i=from-;i>=to;i--)
b[i+len]=b[i];
for(int i=to,j=;j<=len;i++,j++)
b[i]=c[j];
}else if(from<to){
for(int i=from+len;i<=to;i++)
b[i-len]=b[i];
for(int i=to,j=len;j>=;i--,j--)
b[i]=c[j];
}
}
void recover(int from,int len, int to){
if(from<to){
ope(to-len+,len,from);
}else if(to<from){
ope(to,len,from+len-);
}
} bool opeti(int F,int L,int to){
if(F!=)
if(b[F-]+==b[F])
return false;
if(F+L-!=n)
if(b[F+L-]+==b[F+L])
return false;
return true;
}
int h(){
int res=;
for(int i=;i<n;i++)
if(b[i]+!=b[i+])res++;
if(b[n]!=n)res++;
return res;
} bool dfs(int cur){
if(cur==){
if(judge())
return true;
return false;
}
if(h()>*cur)return false;
for(int F=;F<=n;F++){
for(int L=;L<=n-F+;L++){
for(int to=;to<=n;to++){
if(!opeti(F,L,to))continue;
if(to==F)continue;
if(to>F&&to<=F+L-)continue;
if(to<F&&to>=F-L+)continue;
ope(F,L,to);
if(dfs(cur-))
return true;
recover(F,L,to);
}
}
}
return false;
}
int main(){
// freopen("out.txt","w",stdout);
kase=;
while(scanf("%d",&n)!=EOF&&n){
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
b[i]=a[i];
int ans=;
for(int maxd=;;maxd++){
if(dfs(maxd)){
ans=maxd;
break;
}
}
printf("Case %d: %d\n",++kase,ans);
}
return ;
}

【UVA11212 算法竞赛入门经典】 Editing a Book 【IDA*】的更多相关文章

  1. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  2. [刷题]算法竞赛入门经典 3-12/UVa11809

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...

  3. [刷题]算法竞赛入门经典 3-10/UVa1587 3-11/UVa1588

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-10/UVa1587:Box 代码: //UVa1587 - Box #include&l ...

  4. [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 都是<算法竞赛入门经典(第二版)>的题目,标题上没写(第二版) 题目:算法竞赛入门经典 3-7/UVa13 ...

  5. [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa455:Periodic Strings 代码: //UVa455 #inclu ...

  6. [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...

  7. 算法竞赛入门经典训练指南——UVA 11300 preading the Wealth

    A Communist regime is trying to redistribute wealth in a village. They have have decided to sit ever ...

  8. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  9. 算法竞赛入门经典 LA 4329(树状数组)

    题意: 一排有着不同能力值的人比赛,规定裁判的序号只能在两人之间,而且技能值也只能在两人之间 问题: <算法竞赛入门经典-训练指南>的分析: 上代码: #include<iostre ...

随机推荐

  1. thinkphp 使每一个模板页都包括一个header文件和一个footer文件

    在开发的过程中,常常遇到要使每一个模板页都包括一个header文件和一个footer文件.thinkPHP的模板布局为我们提供了一个叫全局配置方式可以解决问题. 1. 在配置文件里开启LAYOUT_O ...

  2. Linux环境下Maven的.m2文件夹

    aven中的.m2文件夹 安装完maven是没有.m2文件夹的.在linux中以.开头的文件夹都是隐藏的.当使用maven命令的时候,maven自动会创建.m2文件夹. 运行命令mvn help:sy ...

  3. winform 控件随页面大小进行自适应

    这个功能网上很多人在问,也有不少人给出过答案,经过实际使用,觉得网上这段代码实现的效果比较好,记录一下 核心代码就是下面这个类 using System; using System.Collectio ...

  4. Windows Driver Kit Version 7.1.0 ( 也就是 7600.16385.1 ) 下载地址

    Windows Driver Kit Version 7.1.0 ( 也就是 7600.16385.1 ) 下载地址 http://download.microsoft.com/download/4/ ...

  5. Socket服务端

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  6. ProducerConsumerQueue

    folly/ProducerConsumerQueue.h The folly::ProducerConsumerQueue class is a one-producer one-consumer ...

  7. Window性能监视器

    Microsoft Web Application Stress Tool 微软的分布式网站性能压力测试工具 Window性能监视器 1.监测IIS连接数量 从“开始”菜单上选择“运行”. 在“打开” ...

  8. python cx_Oracle模块的安装和使用

      $wget http://download.oracle.com/otn/linux/instantclient/10204/basic-10.2.0.4.0-linux-x86_64.zip 3 ...

  9. python执行报错 configparser.NoSectionError: No section: 'section_1'

    场景:请求获取验证码模块regVC.py读取配置文件config.ini时,regVC.py模块单独执行正常,但通过run_all.py模块批量执行时报错,找不到section 解决办法:配置文件路径 ...

  10. linux anaconda 管理 python 包

    1.下载 anaconda https://www.continuum.io/downloads 2.安装anaconda 3.conda install package-name //利用anaco ...