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. ListView之EmptyView

    From:http://blog.csdn.net/xiangqiao123/article/details/17994099 继承ListActivity比较方便 最新开发一个应用程序,需要用到当L ...

  2. 使用Spring报错:No default constructor found;

    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error ...

  3. Linux系统下 Rsync 环境安装搭建

    一.Rsync简介 1.认识 Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 ...

  4. centos6.8 环境一键安装包 nginx配置thinkphp5

    ---恢复内容开始--- lnmp1.4 一键安装包 nginx配置thinkphp5     环境:Nginx1.12.1  PHP5.6  Coentos6.8 修改网站配置文件      ser ...

  5. HDU 6016 Count the Sheep

    Count the Sheep Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  6. sql_q.format

    field_q_insert = 'id, title, number, created,content'sql_q = 'INSERT INTO testquestion ({}) VALUES ( ...

  7. Java 算法-快速幂

    1 什么是快速幂? 快速幂,顾名思义就是快速的求次幂,例如:a^b,普通的算法就是累乘,这样的计算方法的时间复杂度就是O(n),而快速幂的方法使得次幂的计算方法的时间复杂度降低到O(logn).  假 ...

  8. Python开发【模块】:Pygal 绘制直方图

    Pygal Pygal可用来生成可缩放的矢量图形文件,对于需要在尺寸不同的屏幕上显示的图表,这很有用,可以自动缩放,自适应观看者的屏幕 1.Pygal模块安装 ① Windows系统 # Pygal模 ...

  9. Python开发【Django】:重构Admin

    自定义KingAdmin 通过admin样式自己做KingAdmin 提前需知道的model操作 # 获取app名 >>> models.Customer._meta.app_lab ...

  10. 【Loadrunner】使用LR录制HTTPS协议的三种方法

    使用LR录制HTTPS协议的三种方法 一.最简单的方法:浏览器配置打开浏览器,安装证书,配置完成后直接用http协议录制即可(配置完成的标识就是打开网页,不显示安全提示) 二.LR配置修改操作步骤如下 ...