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. Linux下启动tomcat报java.lang.OutOfMemoryError: PermGen space

    一.错误信息 java.lang.reflect.InvocationTargetException    at sun.reflect.NativeMethodAccessorImpl.invoke ...

  2. PermGen space OOM 记录

    前些天线上除出了个OOM问题,今天闲下来记录下: OOM的提示信息是-PermGen space,说明问题出在方法区,方法区存的是什么东西?:类的加载信息.常量.静态变量. 按照方法区的定义:类加载的 ...

  3. 零基础入门学习Python(1)--我和Python的第一次亲密接触

    前言 最近在学习Python编程语言,于是乎就在网上找资源.其中小甲鱼<零基础入门学习Python>试听了几节课,感觉还挺不错,里面的视频都是免费下载,小甲鱼讲话也挺幽默风趣的,所以呢,就 ...

  4. ruby 第五次作业 part 1(分类、排序)

    movies_controller.rb class MoviesController < ApplicationController def movie_params params.requi ...

  5. LeetCode(56)Merge Intervals

    题目 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6], ...

  6. HUAS Summer Contest#4 D题 DP

    Description Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了.要申请国外的任何大学,你都要交纳一定的申请费用,这可是很惊人的 ...

  7. UVA 140 Bandwidth (dfs 剪枝 映射)

    题意: 给定一个n个结点的图G和一个结点的排列, 定义结点i的带宽b(i)为i和相邻结点在排列中的最远距离, 所有b(i)的最大值就是这个图的带宽, 给定G, 求让带宽最小的结点排列. 给定的图 n ...

  8. IDEA 2018集成MyBatis Generator 插件 详解

    1.修改maven的pom文件 只需要将如下依赖添加到pom.xml文件中即可.(注意此处是以plugin的方式,放在<plugins> </plugins>中间即可) < ...

  9. About SQL Server 2016 CPT2

    SQL Server 2016 CTP2已经发布,可以从以下主页进行下载. http://www.microsoft.com/en-us/server-cloud/products/sql-serve ...

  10. 【01】emmet系列之基础介绍

    [01]emmet系列之基础介绍 [02]emmet系列之HTML语法 [03]emmet系列之CSS语法 [04]emmet系列之编辑器 [05]emmet系列之各种缩写 相关网址 官网:http: ...