Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister
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的更多相关文章
- 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 ...
- 【动态规划】Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister
预处理每一层最左侧的1的位置,以及最右侧的1的位置. f(i,0)表示第i层,从左侧上来的最小值.f(i,1)表示从右侧上来. 转移方程请看代码. #include<cstdio> #in ...
- 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 ...
- Codeforces Round #417 (Div. 2) D. Sagheer and Kindergarten(树中判祖先)
http://codeforces.com/contest/812/problem/D 题意: 现在有n个孩子,m个玩具,每次输入x y,表示x孩子想要y玩具,如果y玩具没人玩,那么x就可以去玩,如果 ...
- Codeforces Round #417 (Div. 2)-A. Sagheer and Crossroad
[题意概述] 在一个十字路口 ,给定红绿灯的情况, 按逆时针方向一次给出各个路口的左转,直行,右转,以及行人车道,判断汽车是否有可能撞到行人 [题目分析] 需要在逻辑上清晰,只需要把所有情况列出来即可 ...
- 【二分】Codeforces Round #417 (Div. 2) C. Sagheer and Nubian Market
傻逼二分 #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll ...
- [Codeforces Round#417 Div.2]
来自FallDream的博客,未经允许,请勿转载,谢谢. 有毒的一场div2 找了个1300的小号,结果B题题目看错没交 D题题目剧毒 E题差了10秒钟没交上去. 233 ------- A.Sag ...
- 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 ...
- Codeforces Round #417 (Div. 2) 花式被虐
A. Sagheer and Crossroads time limit per test 1 second memory limit per test 256 megabytes input sta ...
随机推荐
- 在CentOS中使用 yum 安装MongoDB及服务器端配置
转自 http://blog.csdn.net/zhangfeng19880710/article/details/20166853 一.准备工作: 运行yum命令查看MongoDB的包信息 [roo ...
- nexus使用方法
转自:https://www.cnblogs.com/grey-wolf/p/6481166.html centos7搭建nexus maven私服 前置条件: 1.安装jdk,可参考 http: ...
- c++Template 的辨析
1.在c++Template中很多地方都用到了typename与class这两个关键字,而且好像可以替换,是不是这两个关键字完全一样呢? 答:class用于定义类,在模板引入c++后,最初定义模板的方 ...
- 限制ip ssh远程登录
有时候为了服务器的安全考虑,我们可以在服务器上做限制,禁止其他ip地址连接服务器. 方法一:修改ssh配置文件 其实做这个操作很简单,只需要改/etc/ssh/sshd_config配置文件,再最后一 ...
- 常用meta标签及说明
1.charset 定义文档的字符编码 例如: <meta charset="UTF-8"> 2. name 把 content 属性关联到一个名称,其属性有 ...
- Visual Studio 2017正式版离线安装方法
Visual Studio 2017 RTM正式版离线安装及介绍. 首先至官网下载:https://www.visualstudio.com/zh-hans/downloads/ VS 2017 正式 ...
- Tensorflow 实战Google深度学习框架 第五章 5.2.1Minister数字识别 源代码
import os import tab import tensorflow as tf print "tensorflow 5.2 " from tensorflow.examp ...
- Django - 常用配置
一.logging配置 Django项目常用的logging配置 settings.py LOGGING = { 'version': 1, 'disable_existing_loggers': F ...
- ubuntu 安装搜狗
先按照这个选择fcitx: https://blog.csdn.net/qq_40563761/article/details/82664851 然后重启 右上角会出现键盘图片点击选configura ...
- [Luogu4331][Baltic2004]数字序列
原题请见<左偏树的特点及其应用>BY 广东省中山市第一中学 黄源河 luogu 题意 给出序列\(a[1...n]\),要求构造一个不下降序列\(b[1...n]\)使得\(\sum_{i ...