连环锁

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 1260   Accepted: 403

Description

许多人一定很熟悉九连环(如下图),九个环被串在一起,操作规则如下:第一个(右边)环可以任意装卸,如果第k个环没有被卸掉,而第k个环前边(右边)的所有环都被卸掉,则第k+1个环(第k个环左边的环)可以任意装卸(如果存在的话)。 
用0表示此换被卸掉,1表示此环没有被卸掉,则九连环的每个状态可以用一个长度为9的二进制串来表示,如:111111001经过一次操作可以变成111111000,也可以变成111111011,111111111经过一次操作可以变成111111110,也可以变成111111101。 

任务描述: 
你现在要操作的是一个n连环,n为正整数,给出n连环的两种状态,计算出从第一种状态变换到第二种状态所需要的最少步数。 

Input

第一行是一个正整数m,表示有m组测试数据。 
每组测试数据一共3行,第一行是一个正整数n (0 < n < 128),后两行每一行描述一种状态,n个数(0或1),用空格隔开。 

Output

对于每一组测试数据输出一行,一个非负整数,表示从第一种状态变换到第二种状态所需要的最少步数。

Sample Input

2
3
0 0 0
1 0 0
4
1 0 0 0
0 1 1 0

Sample Output

7
11

Source

Position

http://poj.org/problem?id=1832

Solution

【Poj1090】Chain

看下这道题基本就会做了,一个状态到另一个状态=(一个状态→ 0)-(一个状态→0),加些高精度减法,与比大小即可

Code

// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is. #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define MOD 1000000007
#define INF 1e9
using namespace std;
typedef long long LL;
const int MAXN=;
inline int max(int &x,int &y) {return x>y?x:y;}
inline int min(int &x,int &y) {return x<y?x:y;}
inline int gi() {
register int w=,q=;register char ch=getchar();
while((ch<''||ch>'')&&ch!='-')ch=getchar();
if(ch=='-')q=,ch=getchar();
while(ch>=''&&ch<='')w=w*+ch-'',ch=getchar();
return q?-w:w;
}
const int __bmod__=;
struct BN{
int a[];
BN(){memset(a,,sizeof(a));}
int& operator [](int n){return a[n];}
void get(int n){
memset(a,,sizeof(a));
a[]=n;if(a[])a[]=;
while(a[a[]+]){a[a[]+]=a[a[]]/__bmod__;a[a[]++]%=__bmod__;}
}
bool operator <(BN b) const{
if(a[]<b[])return ;
if(a[]>b[])return ;
for(int i=a[];i>=;i--){
if(a[i]>b[i])return ;
if(a[i]<b[i])return ;
}
return ;
}
BN operator -(BN b) const{
BN ans=*this;int q=;
if(ans<b)swap(ans,b),q=-;
for(int i=;i<=ans[];i++){
ans[i]=ans[i]-b[i];
if(ans[i]<){ans[i+]--;ans[i]+=__bmod__;}
}
while(ans[]&&!ans[ans[]])ans[]--;
for(int i=;i<=ans[];i++)ans[i]*=q;
return ans;
}
BN operator +(BN b) const{
b[]=max(a[],b[]);
for(int i=;i<=b[];i++){
b[i]+=a[i];
if(b[i]>=__bmod__){b[i+]+=b[i]/__bmod__;b[i]%=__bmod__;}
}
if(b[b[]+])b[]++;
return b;
}
BN operator *(BN b) const{
BN ans;
ans[]=a[]+b[]-;
for(int i=;i<=a[];i++)
for(int o=;o<=b[];o++){
int now=i+o-;
ans[now]+=a[i]*b[o];
}
for(int i=;i<=ans[];i++)if(ans[i]>=__bmod__){ans[i+]+=ans[i]/__bmod__;ans[i]%=__bmod__;}
if(ans[ans[]+])ans[]++;
return ans;
}
void print(){printf("%d",a[a[]]);for(int i=a[]-;i>=;i--)printf("%.5d",a[i]);printf("\n");}
}now,f[],o,t,up,mu;
int a[MAXN];
int main()
{
freopen("1832.in","r",stdin);
freopen("1832.out","w",stdout);
int T=gi();
while(T--){
int n=gi();
for(int x=;x<;x++){
for(int i=n;i>=;i--)a[i]=gi();
f[x].get(a[]),o.get(-a[]),t.get(),up.get(),mu.get();
for(int i=;i<=n;i++){
if(a[i])
now=f[x],f[x]=o+t+up,o=now;
else o=o+t+up;
t=t*mu+up;
}
}
if(f[]<f[])
(f[]-f[]).print();
else (f[]-f[]).print();
}
return ;
}

【Poj 1832】连环锁的更多相关文章

  1. hdu1816 + POJ 2723开锁(二分+2sat)

    题意:      有m层门,我们在最外层,我们要一层一层的进,每一层上有两把锁,我们只要开启其中的一把们就会开,我们有n组钥匙,每组两把,我们只能用其中的一把,用完后第二把瞬间就会消失,问你最多能开到 ...

  2. mysql 5.6 原生Online DDL解析

    http://seanlook.com/2016/05/24/mysql-online-ddl-concept/ 做MySQL的都知道,数据库操作里面,DDL操作(比如CREATE,DROP,ALTE ...

  3. Online DDL与pt-online-schema-change

    http://seanlook.com/2016/05/24/mysql-online-ddl-concept/ http://seanlook.com/2016/05/27/mysql-pt-onl ...

  4. ACM一些题目

    Low Power 先二分答案,可以通过调整证明同一台机器选的两个芯片必然是提供能量数值相邻的两个.所以再贪心一下就可以了. 时间复杂度\(O(n \log n)\). Factors 假设\(k\) ...

  5. 关于MySQL Online DDL

    1. Online DDL 在 MySQL 5.1 (带InnoDB Plugin)和5.5中,有个新特性叫 Fast Index Creation(下称 FIC),就是在添加或者删除二级索引的时候, ...

  6. mysql 5.6 在线 DDL

    原文链接地址:http://seanlook.com/2016/05/24/mysql-online-ddl-concept/ 做MySQL的都知道,数据库操作里面,DDL操作(比如CREATE,DR ...

  7. (转)mysql 5.6 原生Online DDL解析

    做MySQL的都知道,数据库操作里面,DDL操作(比如CREATE,DROP,ALTER等)代价是非常高的,特别是在单表上千万的情况下,加个索引或改个列类型,就有可能堵塞整个表的读写. 然后 mysq ...

  8. (day30)GIL + 线程相关知识点

    目录 昨日内容 进程互斥锁 队列 进程间通信 生产者与消费者模型 线程 什么是线程 为什么使用线程 创建线程的两种方式 线程对象的属性 线程互斥锁 今日内容 GIL全局解释器锁 多线程的作用 计算密集 ...

  9. Java并发之synchronized关键字和Lock接口

    欢迎点赞阅读,一同学习交流,有疑问请留言 . GitHub上也有开源 JavaHouse,欢迎star 引用 当开发过程中,我们遇到并发问题.怎么解决? 一种解决方式,简单粗暴:上锁.将千军万马都给拦 ...

随机推荐

  1. linux学习系列博客地址汇总

    2018-09-28 16:03:43 CentOS7 yum命令:这是一个用来管理rpm包进行自动化安装的C/S模式的一个程序. CentOS7(无图形界面)支持中文显示的办法:系统安装好之后,有可 ...

  2. 启发式合并CodeForces - 1009F

    E - Dominant Indices CodeForces - 1009F You are given a rooted undirected tree consisting of nn vert ...

  3. Redis 压缩存储的配置

    如题,redis是采用了ziplist 元素在不足一定数量时采用压缩存储 hash: zset: list: 如上图所示: ziplist-entries:最大元素数量(即存储了多少个元素) zipl ...

  4. //……关于promise

    什么是promise? promise 翻译成中文的意思是 "承诺" ,一个承诺说出去了说明他是进行中的,承诺兑现了代表成功,没有兑现代表失败了. promise 对象的状态一旦发 ...

  5. QueryParser

    [概述] 其他工具类使用比较方便,但不够灵活.QueryParser也实现了较多的匹配方式. [QueryParser的应用] /** * 使用QueryParser进行查询 * @throws Pa ...

  6. 九度oj 题目1206:字符串连接

    题目1206:字符串连接 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:5117 解决:2373 题目描述: 不借用任何字符串库函数实现无冗余地接受两个字符串,然后把它们无冗余的连接起来 ...

  7. .NET一般处理程序如何获取AJAX传递的参数

    POST的话 要用 HttpContext.Request.Form 和 HttpContext.Request.Params[""]   GET对应HttpContext.Req ...

  8. Gym100812 L 扩展欧几里得

    L. Knights without Fear and Reproach time limit per test 2.0 s memory limit per test 256 MB input st ...

  9. csu - 1538: Shopping (贪心)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1538 很奇妙的一个题,开始没有思路.问了别人才知道. 题目的意思可以理解成上图中,从0点开始向右走 ...

  10. hdu - 2645 find the nearest station (bfs水)

    http://acm.hdu.edu.cn/showproblem.php?pid=2645 找出每个点到距离最近的车站的距离. 直接bfs就好. #include <cstdio> #i ...