Chain

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3414   Accepted: 1126

Description

Byteland had not always been a democratic country. There were also black pages in its book of history. One lovely day general Bytel − commander of the junta which had power over Byteland −− decided to finish the long−lasting time of war and released imprisoned activists of the opposition. However, he had no intention to let the leader Bytesar free. He decided to chain him to the wall with the bytish chain. It consists of joined rings and the bar fixed to the wall. Although the rings are not joined with the bar, it is hard to take them off.
'General, why have you chained me to the prison walls and did not let rejoice at freedom!' cried Bytesar. 
'But Bytesar, you are not chained at all, and I am certain you are able to take off the rings from the bar by yourself.' perfidiously answered general Bytel, and he added 'But deal with that before a clock strikes the cyber hour and do not make a noise at night, otherwise I will be forced to call Civil Cyber Police.' 
Help Bytesar! Number the following rings of the chain with integers 1,2,...,n. We may put on and take off these rings according to the following rules: 
.only one ring can be put on or taken off from the bar in one move, 
.the ring number 1 can be always put on or taken off from the bar, 
.if the rings with the numbers 1,...,k−1 (for 1<= k < n) are taken off from the bar and the ring number k is put on, we can put on or take off the ring number k+1. 
Write a program which: 
.reads from std input the description of the bytish chain, 
.computes minimal number of moves necessary to take off all rings of the bytish chain from the bar, 
.writes the result to std output.

Input

In the first line of the input there is written one integer n, 1 <= n <= 1000. In the second line there are written n integers o1,o2,...,on (each of them is either 0 or 1) separated by single spaces. If oi=1, then the i−th ring is put on the bar, and if oi=0, then the i−th ring is taken off the bar.

Output

The output should contain exactly one integer equal to the minimal number of moves necessary to take off all the rings of the bytish chain from the bar.

Sample Input

4
1 0 1 0

Sample Output

6

Source

Position

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

Solution

这不是中国的九连环吗?大意:一次操作改变一位,第一位可以随便改,第k(k>1)位要改时当且仅当k-1为1,且1~k-2为0

鬼题啊~

1 格雷码  这个人写得很好:紫忆

2 递推,Dp

  f(n)表示将字符串1 - n位全部变为0所需的最小步骤

   o(n)表示,将字符串1 - (n - 1)位全部变为0,且第n位为1所需的最小步骤

   t(n)表示,当1 - (n - 1)位全为0且第n为为1时,将1-n位全部变为0所需的最小步骤

递推关系如下:

   f(1) =  a[1];

   o(1) = 1 - a[1];

   t(1) = 1;

f(n) = o(n - 1) + 1 + t(n - 1), 当a[n] = 1

f(n - 1), 当a[n] = 0

o(n) = f(n - 1),  当a[n] = 1

o(n - 1) + 1 + t(n - 1); 当 a[n] = 0

t(n) = 2 * t(n - 1) + 1;

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__;}
}
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]);}
}now,f,o,t,up,mu;
int a[MAXN];
int main()
{
freopen("1090.in","r",stdin);
freopen("1090.out","w",stdout);
int n=gi();
for(int i=;i<=n;i++)a[i]=gi();
f.get(a[]),o.get(-a[]),t.get(),up.get(),mu.get();
for(int i=;i<=n;i++){
if(a[i])
now=f,f=o+t+up,o=now;
else o=o+t+up;
t=t*mu+up;
}
f.print();
return ;
}

【Poj1090】Chain的更多相关文章

  1. 【poj1090】 Chain

    http://poj.org/problem?id=1090 (题目链接) 题意 给出九连环的初始状态,要求将环全部取下需要走多少步. Solution 格雷码:神犇博客 当然递推也可以做. 代码 / ...

  2. .htaccess详解及.htaccess参数说明【转】

    目录(?)[-] htaccess 详解 htaccess rewrite 规则详细说明 RewriteEngine OnOff RewriteBase URL-path RewriteCond Te ...

  3. 【转】.htaccess详解及.htaccess参数说明

    .htaccess文件(或者”分布式配置文件”)提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目录及其所有子目录.作为用户,所能使用的命令受到 ...

  4. 【转】服务器.htaccess 详解以及 .htaccess 参数说明

    htaccess文件(或者”分布式配置文件”)提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目录及其所有子目录.作为用户,所能使用的命令受到限 ...

  5. 【HDU3487】【splay分裂合并】Play with Chain

    Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it ...

  6. 【33.33%】【codeforces 608C】Chain Reaction

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 论文阅读(Xiang Bai——【CVPR2012】Detecting Texts of Arbitrary Orientations in Natural Images)

    Xiang Bai--[CVPR2012]Detecting Texts of Arbitrary Orientations in Natural Images 目录 作者和相关链接 方法概括 方法细 ...

  8. 【原】javascript执行环境及作用域

    最近在重读<javascript高级程序设计3>,觉得应该写一些博客记录一下学习的一些知识,不然都忘光啦.今天要总结的是js执行环境和作用域. 首先来说一下执行环境 一.执行环境 书上概念 ...

  9. 常用的机器学习&数据挖掘知识点【转】

    转自: [基础]常用的机器学习&数据挖掘知识点 Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Le ...

随机推荐

  1. CentOS7 export命令

    一.windows下的环境变量 在windows系统下,很多软件安装都需要配置环境变量,比如安装jdk,假如你没有配置环境变量,那么在非软件安装的目录下使用javac命令,系统将会报这不是系统内部命令 ...

  2. 零基础入门学习Python(26)--字典:当索引不好用时2

    知识点 删除字典元素 能删单一的元素也能清空字典,清空只需一项操作. 显示删除一个字典用del命令,如下: >>> dict1 = {'a':1,'b':2,'c':3} >& ...

  3. PHP实现QQ第三方登录的方法

    前言: PHP实现QQ快速登录,罗列了三种方法 方法一:面向过程,回调地址和首次触发登录写到了一个方法页面[因为有了if做判断], 方法二,三:面向对象 1.先调用登录方法,向腾讯发送请求,2.腾讯携 ...

  4. [Android] java代码无错误,但跳转失败

    今天在调代码的时候,出现了这样的问题,我晕了半天,才找到解决办法. 查看日志发现:Initialize Binary Program Cache: Load Failed 从来没见过这种问题,Java ...

  5. [bzoj1208][HNOI2004][宠物收养所] (平衡树)

    Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...

  6. 拥抱变革(More Fearless Change)

    今天从大敏捷群中了解到Tid2017的一个讲演,一位敏捷教练,组织变革的专著的作者Linda的讲演. <More Fearless Change-Strategy for Making Your ...

  7. 到达时间自动点击按钮弹出提示并跳转【JavaScript实现】

    原文发布时间为:2008-10-11 -- 来源于本人的百度文章 [由搬家工具导入] 其实我本来是想 做 在线考试的时候 规定时间到达时候自动交卷的,就想到这个例子了。。。。 代码: <html ...

  8. codevs2597 团伙

    题目描述 Description 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友 ...

  9. ci框架(codeigniter)Email发送邮件、收件人、附件、Email调试工具

        ci框架(codeigniter)Email发送邮件.收件人.附件.Email调试工具 Email 类         CodeIgniter 拥有强大的 Email 类来提供如下的功能: 多 ...

  10. Educational Codeforces Round 41 B、C、D

    http://codeforces.com/contest/961 B题 可以将长度为k的连续区间转化成1 求最大和 解析 简单尺取 #include <stdio.h> #include ...