BZOJ1330 : Editing a Book
注意到答案不超过$5$,因此可以考虑BFS求出距离起始态或者终止态不超过$2$的所有状态。
设它们到起始态、终止态的距离分别为$f[S],g[S]$,则$ans=\min(5,f[S]+g[S])$。
时间复杂度$O(n^6\log(n!))$。
#include<cstdio>
#include<algorithm>
using namespace std;
typedef unsigned long long ll;
const int N=12,M=363000;
int n,i,j,k,a[N],len[N],S,x,z,v[M],g[M],h,t,q[M],ans;ll f[N][M];
inline ll encode(){
ll t=0;
for(int i=0;i<n;i++)t=t<<4|a[i];
return t;
}
inline int getid(ll x){
int l=0,r=len[n]-1;
while(l<=r){
int mid=(l+r)>>1;
if(f[n][mid]==x)return mid;
if(f[n][mid]<x)l=mid+1;else r=mid-1;
}
}
inline void ext(int x){
if(~v[x])return;
v[q[++t]=x]=z;
}
int main(){
for(n=2;n<=9;n++){
for(i=0;i<n;i++)a[i]=i;
do{f[n][len[n]++]=encode();}while(next_permutation(a,a+n));
sort(f[n],f[n]+len[n]);
}
while(~scanf("%d",&n)){
if(!n)return 0;
for(i=0;i<n;i++)scanf("%d",&a[i]),a[i]--;
if(n==1){puts("0");continue;}
S=getid(encode());
h=1,t=0;
for(i=0;i<len[n];i++)v[i]=-1;
z=0;
ext(S);
while(h<=t){
z=v[x=q[h++]]+1;
if(z>2)continue;
ll O=f[n][x];
for(i=1;i<n;i++)for(j=i;j<n;j++){
ll U=(1ULL<<((j-i+2)*4))-1,F=O;
int L=(j-i+1)*4;
for(k=4*(i-1);k>=0;k-=4){
ll A=(F>>k)&U;
F^=(A^((A>>4)|(A&15)<<L))<<k;
ext(getid(F));
}
}
}
for(i=0;i<len[n];i++)g[i]=v[i],v[i]=-1;
h=1,t=z=0;
ext(0);
while(h<=t){
z=v[x=q[h++]]+1;
if(z>2)continue;
ll O=f[n][x];
for(i=1;i<n;i++)for(j=i;j<n;j++){
ll U=(1ULL<<((j-i+2)*4))-1,F=O;
int L=(j-i+1)*4;
for(k=4*(i-1);k>=0;k-=4){
ll A=(F>>k)&U;
F^=(A^((A>>4)|(A&15)<<L))<<k;
ext(getid(F));
}
}
}
for(ans=5,i=0;i<len[n];i++)if(~v[i]&&~g[i])ans=min(ans,v[i]+g[i]);
printf("%d\n",ans);
}
return 0;
}
BZOJ1330 : Editing a Book的更多相关文章
- vim - save current file with a new name but keep editing current file
http://superuser.com/questions/414110/vim-save-a-file-as-a-different-filename-but-keep-w-as-the-curr ...
- Simultaneous Tag Editing in IntelliJ IDEA 14.1
If you're involved in web development and, for some reason, you haven't given a ride to IntelliJ IDE ...
- Poisson Image Editing
说起泊松,可以顺便提及一下泊松同学的老师,拉普拉斯.学图像或是信号的,一定对拉普拉斯算子和拉普拉斯卷积很熟悉.在泊松图像融合出现之前,也有一种叫Laplacian pyramid blending的融 ...
- 错误异常 (1)Android Studio错误提示:Gradle project sync failed. Basic functionality (eg. editing, debugging) will not work properly
[已解决]Android Studio错误提示:Gradle project sync failed. Basic functionality (eg. editing, debugging) wil ...
- 【JQGRID DOCUMENTATION】.学习笔记.6.Editing:Common Rules
1 公共编辑属性 要在grid中显示数据的一个关键原因是能简单快速地编辑它.jgGrid提供三种编辑方式: cell editing:编辑指定cell inline editing:编辑同一行的几个c ...
- 【JQGRID DOCUMENTATION】.学习笔记.5.Form Editing
JqGrid支持为view ,add, edit,delete, search创建一个on the fly的form.这使得Modal Boxes和行内编辑能够在程序用结合.毕竟,行内编辑只能用在已经 ...
- UVa 10115 Automatic Editing
字符串题目就先告一段落了,又是在看balabala不知道在说些什么的英语. 算法也很简单,用了几个库函数就搞定了.本来还担心题里说的replace-by为空的特殊情况需要特殊处理,后来发现按一般情况处 ...
- Editing and Deleting Data
Editing and Deleting Data In the previous chapter we've come to learn how we can use the zend-form a ...
- [r]Seven habits of effective text editing
Seven habits of effective text editing(via) Bram Moolenaar November 2000 If you spend a lot of time ...
随机推荐
- 实现开发板与ubuntu的共享--根文件系统NFS--Samba共享【sky原创】
虚拟机要选择桥接,并且禁用有线和无线网卡,开启本地连接,本地连接属性要写如下: ip地址是在连上板子后,windows cmd 下 ipconfig得出的 板子的网线最好连接交换机或者 ...
- jdk写webservice
jdk写webservice 1.定义一个需要发布的类,使用@WebService注解. 2.需要发布的方法可以不用@WebMethod注解,如果需要改变访问方法名,可用@WebMethod修改. 3 ...
- LeetCode(40):组合总和 II
Medium! 题目描述: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数 ...
- appium自动化测试之元素定位
方法一 使用SDK中附带的uiautomatorviewer来定位 在SDK安装目录下的tools下有个uiautomatorviewer.bat批处理文件点击运行 运行后(注意appium desk ...
- java根据word模板导出word文件
1.word模板文件处理,如下图所示在word 文档中填值的地方写入占位变量 2.将word文档另存为xml文件.编辑如下图,找到填写的占位,修改为${bcrxm}格式 3.将文件后缀名改为.ftl文 ...
- 判断iframe加载完成、用于当ifame加载完成时执行一些操作
window.frames["iframec"].addEventListener( "load", function(){ window.frames[&qu ...
- Linux下搭建ruby on rails环境
要搭建的东西:Ruby 1.8.7,Rails 2.3.8,rubygem 1.3.7 1.安装ruby sudo apt-get install ruby-full 2.安装rubygem sudo ...
- nexus私服常用设置
https://www.jianshu.com/p/7a09915675d9 或许用得上. settings.xml <server> <!-- 服务器密码 --> <i ...
- SpringBank 开发日志 重新设计Action调用Service的参数传递 使用泛型解决类型转换问题
之前想的比较简单,请求到达controller的时候,传给action的参数没有经过任何封装,就是一个Map.然后action再调用service的时候,传递的参数也是map @Controller ...
- P3763 [TJOI2017]DNA
链接:https://www.luogu.org/problemnew/show/P3763 题解: 挺水的一题后缀数组 枚举每一个开头用后缀数组判断能否在3次内匹配完