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 ...
随机推荐
- scn 时间
Scn转换成时间: select to_char(scn_to_timestamp(3998591352171),'YYYY-MM-DD HH24:MI:SS') from dual: 时间转换成sc ...
- 使用rpm包安装lamp环境
前提: 是你的centos能联网,或者有本地的yum仓库 或者配置通过代理上网 vim /etc/yum.conf 加入如下内容 proxy=http://192.168.11.82:808 1.通过 ...
- linux网络设备驱动
Linux网络设备驱动 Linux网络驱动程序的体系结构可划分为4个层次.Linux内核源代码中提供了网络设备接口及以网络子系统的上层的代码,移植特定网络硬件的驱动程序的主要工作就是完成设备驱动功能层 ...
- LeetCode(33):搜索旋转排序数组
Medium! 题目描述: 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值 ...
- %SystemRoot%
%SystemRoot%即C:\windows
- c++ primer 学习杂记3【标准IO库】
第8章 标准IO库 发现书中一个错误,中文版p248 流状态的查询和控制,举了一个代码例子: int ival; // read cin and test only for EOF; loop is ...
- SqlServer基础语句练习(一)
学了不少东西,感觉自己的sql语句还是很不好,从基础学起吧. 来一段sql脚本: create database tongji go use tongji go create table studen ...
- android Unable to inflate view tag without class attribute
定位 到 问题 是 布局文件出错, Unable to inflate view tag without class attribute 错误 原因 <view android:layout ...
- 设计原则:开-闭原则(Open-Closed Principle, OCP)
开-闭原则就是软件实体应当对扩展开放,对修改关闭.(Software entities should be open for extension,but closed for modification ...
- 《Java程序性能优化》之设计优化
豆瓣读书:http://book.douban.com/subject/19969386/ 第一章 Java性能调优概述 1.性能的参考指标 执行时间: CPU时间: 内存分配: 磁盘吞吐量: 网络吞 ...