【UVA11212 算法竞赛入门经典】 Editing a Book 【IDA*】
题意
你有一篇由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*】的更多相关文章
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- [刷题]算法竞赛入门经典 3-12/UVa11809
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...
- [刷题]算法竞赛入门经典 3-10/UVa1587 3-11/UVa1588
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-10/UVa1587:Box 代码: //UVa1587 - Box #include&l ...
- [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 都是<算法竞赛入门经典(第二版)>的题目,标题上没写(第二版) 题目:算法竞赛入门经典 3-7/UVa13 ...
- [刷题]算法竞赛入门经典 3-4/UVa455 3-5/UVa227 3-6/UVa232
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa455:Periodic Strings 代码: //UVa455 #inclu ...
- [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...
- 算法竞赛入门经典训练指南——UVA 11300 preading the Wealth
A Communist regime is trying to redistribute wealth in a village. They have have decided to sit ever ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- 算法竞赛入门经典 LA 4329(树状数组)
题意: 一排有着不同能力值的人比赛,规定裁判的序号只能在两人之间,而且技能值也只能在两人之间 问题: <算法竞赛入门经典-训练指南>的分析: 上代码: #include<iostre ...
随机推荐
- Maven中plugins和pluginManagement的区别
pluginManagement是表示插件声明,即你在项目中的pluginManagement下声明了插件,Maven不会加载该插件,pluginManagement声明可以被继承. pluginMa ...
- 随着tomcat一起启动一个线程
package test; import javax.servlet.*; public class MyCode implements ServletContextListener { //当Tom ...
- TOMCATE8下面项目启动问题
1.将servlet-api.jar替换项目中的servlet-api2.4 2.<servlet> <servlet-name>dwr-invoker&l ...
- 2DAY初识python
一.变量 1 什么是变量之声明变量 #变量名=变量值 age=18 gender1='male' gender2='female' 2 为什么要有变量 变量作用:“变”=>变化,“量”=> ...
- spring面试资料
* Spring的优点有什么? 1. Spring是分层的架构,你可以选择使用你需要的层而不用管不需要的部分 2. Spring是POJO编程,POJO编程使得可持续构建和可测试能力提高 ...
- FC Switch sfpshow
sfpshow - fault-finding on Brocade Fibre Channel Switches So you've hit a situation where a Fibre Ch ...
- java代码----------计算器代码
总结: 很多不完善—— package com.rue; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.a ...
- DOM的基本概念
1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 2.Window对象操作 一.属性和方法: 属性(值或者子对象): op ...
- nginx限制请求之二:(ngx_http_limit_req_module)模块
相关文章: <高可用服务设计之二:Rate limiting 限流与降级> <nginx限制请求之一:(ngx_http_limit_conn_module)模块> <n ...
- jquery发送数组
假设keywordid_list是一个javascript数组,将它发送到keywordbatchsetting.php. $.ajax({ url:"keywordbatchsetting ...