【Poj1090】Chain
Chain
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 Sample Output 6 Source |
Position
http://poj.org/problem?id=1090
Solution
这不是中国的九连环吗?大意:一次操作改变一位,第一位可以随便改,第k(k>1)位要改时当且仅当k-1为1,且1~k-2为0
鬼题啊~
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的更多相关文章
- 【poj1090】 Chain
http://poj.org/problem?id=1090 (题目链接) 题意 给出九连环的初始状态,要求将环全部取下需要走多少步. Solution 格雷码:神犇博客 当然递推也可以做. 代码 / ...
- .htaccess详解及.htaccess参数说明【转】
目录(?)[-] htaccess 详解 htaccess rewrite 规则详细说明 RewriteEngine OnOff RewriteBase URL-path RewriteCond Te ...
- 【转】.htaccess详解及.htaccess参数说明
.htaccess文件(或者”分布式配置文件”)提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目录及其所有子目录.作为用户,所能使用的命令受到 ...
- 【转】服务器.htaccess 详解以及 .htaccess 参数说明
htaccess文件(或者”分布式配置文件”)提供了针对目录改变配置的方法, 即,在一个特定的文档目录中放置一个包含一个或多个指令的文件, 以作用于此目录及其所有子目录.作为用户,所能使用的命令受到限 ...
- 【HDU3487】【splay分裂合并】Play with Chain
Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it ...
- 【33.33%】【codeforces 608C】Chain Reaction
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 论文阅读(Xiang Bai——【CVPR2012】Detecting Texts of Arbitrary Orientations in Natural Images)
Xiang Bai--[CVPR2012]Detecting Texts of Arbitrary Orientations in Natural Images 目录 作者和相关链接 方法概括 方法细 ...
- 【原】javascript执行环境及作用域
最近在重读<javascript高级程序设计3>,觉得应该写一些博客记录一下学习的一些知识,不然都忘光啦.今天要总结的是js执行环境和作用域. 首先来说一下执行环境 一.执行环境 书上概念 ...
- 常用的机器学习&数据挖掘知识点【转】
转自: [基础]常用的机器学习&数据挖掘知识点 Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Le ...
随机推荐
- CSU1018: Avatar
1018: Avatar Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 841 ...
- Dash Speed
题目大意: 比特山是比特镇的飙车圣地.在比特山上一共有n 个广场,编号依次为1 到n,这些广场之间通过n - 1 条双向车道直接或间接地连接在一起,形成了一棵树的结构.因为每条车道的修建时间以及建筑材 ...
- [Python3网络爬虫开发实战] 3.2.2-高级用法
在前一节中,我们了解了requests的基本用法,如基本的GET.POST请求以及Response对象.本节中,我们再来了解下requests的一些高级用法,如文件上传.cookie设置.代理设置等. ...
- Buffer.alloc()
Buffer.alloc(size[, fill[, encoding]]) Node.js FS模块方法速查 size {Number} fill {Value} 默认:undefined enco ...
- YOLOv3测试命令
一.老规矩 在darknet\build\darknet\x6下按住shift键,点击鼠标右键选择“在此处打开Powershell 窗口(s)” 二.测试图片命令: .\darknet detect ...
- 集训第六周 矩阵快速幂 K题
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- Codeforces Round #258 (Div. 2) D
D. Count Good Substrings time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 【BZOJ2330】糖果(差分约束系统,强连通分量,拓扑排序)
题意: 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配糖 ...
- Ubuntu 16.04通过Magent搭建Memcached集群(转)
一.下载Magent 官网:https://code.google.com/archive/p/memagent/downloads 离线版本:(链接: https://pan.baidu.com/s ...
- MongoDB小结01 - MongoDB简介
我们为什么要去学习MongoDB MongoDB是一种强大.灵活.可扩展的数据存储方式. 丰富的数据模型 MongoDB是面向文档的数据库,不是关系型数据库.它将原来'行'(row)的概念换成了更加灵 ...