http://codeforces.com/contest/812/problem/B

题意:

有n层楼,每层楼有m个房间,1表示灯开着,0表示灯关了。最两侧的是楼梯。

现在每从一个房间移动到另一个房间需要1时间,走楼梯也需要1时间,求关完所有灯所需的最少时间。

思路:

d【i】【0】表示第i层楼关完灯后从左边楼梯上楼的最少时间,d【i】【1】代表右楼梯。

预处理一下,开两个数组保存每层楼开灯房间的最左边的值和最右边的值,这样也就确定了要走的范围。

对于上面灯全部关了的楼层,可以不用去考虑它。

有个错误让我wro了好久!!

就是在dp的时候,如果这是中间的一个全部关了灯的楼层,需要特殊计算,不然计算d【i】【1】是计算错误的。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int,long long> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n,m; int l[maxn],r[maxn];
int d[maxn][];
char g[maxn][maxn]; int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(l,,sizeof(l));
memset(r,,sizeof(r)); for(int i=n;i>=;i--)
{
scanf("%s",g[i]);
{
for(int j=;j<m+;j++)
{
if(g[i][j]=='')
{
r[i]=j;
if(l[i]==) l[i]=j;
}
}
}
} for(int i=n;i>=;i--)
{
if(l[i]== && r[i]==) n--;
else break;
} if(n==)
{
puts("");
continue;
} if(n==)
{
printf("%d\n",r[]);
continue;
} memset(d,INF,sizeof(d)); d[][]=*r[]+;
d[][]=m++; for(int i=;i<n;i++)
{
if(l[i]== && r[i]==)
{
d[i][]=d[i-][]+;
d[i][]=d[i-][]+;
}
else
{
d[i][]=min(d[i-][]+*r[i],d[i-][]+m+)+;
d[i][]=min(d[i-][]+m+,d[i-][]+*(m+-l[i]))+;
}
} int ans=min(d[n-][]+r[n],d[n-][]+m+-l[n]); printf("%d\n",ans);
}
return ;
}

Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister的更多相关文章

  1. Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister —— DP

    题目链接:http://codeforces.com/problemset/problem/812/B B. Sagheer, the Hausmeister time limit per test ...

  2. 【动态规划】Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister

    预处理每一层最左侧的1的位置,以及最右侧的1的位置. f(i,0)表示第i层,从左侧上来的最小值.f(i,1)表示从右侧上来. 转移方程请看代码. #include<cstdio> #in ...

  3. Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. Codeforces Round #417 (Div. 2) D. Sagheer and Kindergarten(树中判祖先)

    http://codeforces.com/contest/812/problem/D 题意: 现在有n个孩子,m个玩具,每次输入x y,表示x孩子想要y玩具,如果y玩具没人玩,那么x就可以去玩,如果 ...

  5. Codeforces Round #417 (Div. 2)-A. Sagheer and Crossroad

    [题意概述] 在一个十字路口 ,给定红绿灯的情况, 按逆时针方向一次给出各个路口的左转,直行,右转,以及行人车道,判断汽车是否有可能撞到行人 [题目分析] 需要在逻辑上清晰,只需要把所有情况列出来即可 ...

  6. 【二分】Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market

    傻逼二分 #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll ...

  7. [Codeforces Round#417 Div.2]

    来自FallDream的博客,未经允许,请勿转载,谢谢. 有毒的一场div2 找了个1300的小号,结果B题题目看错没交  D题题目剧毒 E题差了10秒钟没交上去. 233 ------- A.Sag ...

  8. Codeforces Round #417 (Div. 2)A B C E 模拟 枚举 二分 阶梯博弈

    A. Sagheer and Crossroads time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. Codeforces Round #417 (Div. 2) 花式被虐

    A. Sagheer and Crossroads time limit per test 1 second memory limit per test 256 megabytes input sta ...

随机推荐

  1. 在MacOS上搭建Vulhub漏洞平台环境

    一.安装python3和docker brew install python3 brew cask install docker sudo pip3 install docker-compose 二. ...

  2. 微信小游戏 egret.getDefinitionByName获取不到

    使用getDefinitionByName获取类定义 输出为null,获取不了 增加window["LoadingUI"] = LoadingUI 获取成功 总结: 这样无论是游戏 ...

  3. linux下远程服务器批量执行命令及SFTP上传文件 -- python实现

    之前写过一个python远程执行命令的脚本,但在一个性能测试中,要将程序批量分发到不同服务器,程序无法使用,再将之前的脚本更新,加入批量上传的功能.之前脚本地址:http://www.cnblogs. ...

  4. Python - 3.6 学习二

    Python 的高级特性 切片 对于指定索引范围取值的操作,Python提供了slice方法,类似于Excel中数据透视表的切片器. >>> L = ['Michael', 'Sar ...

  5. CentOS7 部署tomcat

    一.环境说明: 在 CentOS7下面部署tomcat7 . 二.tomcat部署 1.新建tomcat目录  mkdir tomcat 2.上传tomcat 安装文件   apache-tomcat ...

  6. 170802、Elasticsearch5.2.2 安装问题记录

    使用Elasticsearch5.2.2 必须安装jdk1.8 [elsearch@vm-mysteel-dc-search01 bin]$ java -version java version &q ...

  7. 修改Yii2的默认语言language为中文zh-CN的方法

    如果用的语言是en-US的话,网页加载速度很慢,因为要加载国外镜像或者链接什么的,使用zh-CN就直接加载国内链接或者CDN,速度比较快: ------------------------------ ...

  8. 配置oem

    [oracle@kaifai ~]$ export ORACLE_SID=dbking[oracle@kaifai ~]$ export ORACLE_SID=kaifai[oracle@kaifai ...

  9. nginx repos

    vim /etc/yum.repos.d/nginx.repo 然后将下面的内容复制进去: [nginx] name=nginx repo baseurl=http://nginx.org/packa ...

  10. SQL Server查看库、表占用空间大小

    转自:https://blog.csdn.net/yenange/article/details/50493580 查询数据文件与日志文件占用情况,查看数据大小,查看库大小 1. 查看数据文件占用(权 ...