【Poj1090】Chain

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=1010;
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=0,q=0;register char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    if(ch=='-')q=1,ch=getchar();
    while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
    return q?-w:w;
}
const int __bmod__=100000;
struct BN{
    int a[100];
    BN(){memset(a,0,sizeof(a));}
    int& operator [](int n){return a[n];}
    void get(int n){
         memset(a,0,sizeof(a));
         a[1]=n;if(a[1])a[0]=1;
         while(a[a[0]+1]){a[a[0]+1]=a[a[0]]/__bmod__;a[a[0]++]%=__bmod__;}
    }
    BN operator +(BN b) const{
        b[0]=max(a[0],b[0]);
        for(int i=1;i<=b[0];i++){
            b[i]+=a[i];
            if(b[i]>=__bmod__){b[i+1]+=b[i]/__bmod__;b[i]%=__bmod__;}
        }
        if(b[b[0]+1])b[0]++;
        return b;
    }
    BN operator *(BN b) const{
        BN ans;
        ans[0]=a[0]+b[0]-1;
        for(int i=1;i<=a[0];i++)
            for(int o=1;o<=b[0];o++){
                int now=i+o-1;
                ans[now]+=a[i]*b[o];
            }
        for(int i=1;i<=ans[0];i++)if(ans[i]>=__bmod__){ans[i+1]+=ans[i]/__bmod__;ans[i]%=__bmod__;}
        if(ans[ans[0]+1])ans[0]++;
        return ans;
    }
    void print(){printf("%d",a[a[0]]);for(int i=a[0]-1;i>=1;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=1;i<=n;i++)a[i]=gi();
    f.get(a[1]),o.get(1-a[1]),t.get(1),up.get(1),mu.get(2);
    for(int i=2;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 0;
}

 

posted @ 2016-10-12 18:04  _Mashiro  阅读(487)  评论(0编辑  收藏  举报