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 ...
随机推荐
- scss语法
SCSS其实就是SASS新语法, 增强了对CSS3语法的支持 1.变量(Variables) /*声明变明*/ $color: #333; $bgcolor:#f36; /*引用变量*/ body { ...
- Windows Phone 页面切换动画
1.首先引用Microsoft.Phone.Toolkit 2.将App.xaml.cs 中的 RootFrame = new PhoneApplicationFrame(); 改成RootFrame ...
- java如何发起https请求
1.写一个SSLClient类,继承至HttpClient import java.security.cert.CertificateException; import java.security.c ...
- git add -A和git add . 的区别
git add -A和 git add . git add -u在功能上看似很相近,但还是有所差别. git add . :他会监控工作区的状态树,使用它会把工作时的所有变化提交到暂存区,包括文件内容 ...
- Redis连接(二)
Redis 命令 Redis 命令用于在 redis 服务上执行操作. 要在 redis 服务上执行命令需要一个 redis 客户端.Redis 客户端在我们之前下载的的 redis 的安装包中. 语 ...
- php最全基础,数组,函数,超全局变量,时间,回话,文件,php操作mysql
共享一份学习php最全基础语法知识的笔记 原文链接:http://www.cnblogs.com/oscn/p/3607757.html:略有修改 http://www.cnblogs.com/l ...
- 分区默认segment大小变化(64k—>8M)
_partition_large_extents和_index_partition_large_extents 参考: http://www.xifenfei.com/2013/08/%E5%88%8 ...
- MySQL Bugs: #34354: Feature request: EXPLAIN ALTER TABLE https://bugs.mysql.com/bug.php?id=34354
MySQL Bugs: #34354: Feature request: EXPLAIN ALTER TABLE https://bugs.mysql.com/bug.php?id=34354 [SQ ...
- centos 系统软件包管理 yum 本地yum配置 扩展源epel rpm 清除yum缓存 yum provides "*/vim" 第十节课
centos 系统软件包管理 yum 本地yum配置 扩展源epel rpm 清除yum缓存 yum provides "*/vim" 第十节课 你不能保证可逆化操 ...
- weiwo.wxmmd.com将您重定向的次数过多。尝试清除 Cookie.
折腾了很久,最后更换PHP版本解决了,我的项目用的tp3.1.2,出现上图问题时的php版本是7.1,换回5.6就没有这个问题.希望能为大家提供一个思路.