Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister —— DP
题目链接:http://codeforces.com/problemset/problem/812/B
1 second
256 megabytes
standard input
standard output
Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.
The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms
on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows
and m + 2 columns, where the first and the last columns represent the stairs, and the m columns
in the middle represent rooms.
Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there
off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help
Sagheer find the minimum total time to turn off all the lights.
Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.
The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100)
— the number of floors and the number of rooms in each floor, respectively.
The next n lines contains the building description. Each line contains a binary string of length m + 2 representing
a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates
that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the
ground floor.
The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.
Print a single integer — the minimum total time needed to turn off all the lights.
2 2
0010
0100
5
3 4
001000
000010
000010
12
4 3
01110
01110
01110
01110
18
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in
the second floor using the left or right stairs.
In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.
In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.
题解:
1.l[i]记录在第i层中,从左往右数,最后一个“1”的位置; r[i]记录在第i层中,从右往左数,最后一个“1”的位置。
2.对于第i层楼的左梯,它可能是从下一层的左梯转移过来的,也可能是从下一层楼的右梯转移过来。对于右梯也一样。
所以:dp[i][j] 表示当到达第i层的j梯(0为左梯,1为右梯)时,所花费的最少步数。
状态转移方程:
{
dp[i][0] = min(dp[i-1][0]+2*l[i]+1, dp[i-1][1]+m+2);
dp[i][1] = min(dp[i-1][1]+2*r[i]+1, dp[i-1][0]+m+2);
}
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 15+10; int n, m, h;
int l[maxn], r[maxn], dp[maxn][2]; void init()
{
scanf("%d%d",&n,&m);
char s[150];
for(int i = n; i>=1; i--)
{
scanf("%s",s);
for(int j = 1; j<=strlen(s)-2; j++)
{
if(s[j]=='1')
{
if(!h) h = i;
if(!r[i]) r[i] = m+1-j;
l[i] = j;
}
}
}
} void solve()
{
dp[0][0] = 0;
dp[0][1] = INF/2;
for(int i = 1; i<h; i++)
{
dp[i][0] = min(dp[i-1][0]+2*l[i]+1, dp[i-1][1]+m+2);
dp[i][1] = min(dp[i-1][1]+2*r[i]+1, dp[i-1][0]+m+2);
}
int ans = min(dp[h-1][0]+l[h], dp[h-1][1]+r[h]);
cout<<ans<<endl;
} int main()
{
init();
solve();
}
Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister —— DP的更多相关文章
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister
http://codeforces.com/contest/812/problem/B 题意: 有n层楼,每层楼有m个房间,1表示灯开着,0表示灯关了.最两侧的是楼梯. 现在每从一个房间移动到另一个房 ...
- 【动态规划】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 #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- [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 ...
随机推荐
- FMDB支持的事务类型
FMDB支持的事务类型 在数据库中,事务可以保证数据操作的完整性.当存在大量并发操作,容易出现死锁问题.在SQLite中,为了解决该问题,提供三种事务模式,分别为DEFFERED.IMMEDIAT ...
- 服务器SSL/TLS快速检测工具TLLSSLed
服务器SSL/TLS快速检测工具TLLSSLed 现在SSL和TLS被广泛应用服务器的数据加密中,如网站的HTTPS服务.所以,在渗透测试中如何快速检测服务器的SSL和TLS配置寻找安全漏洞,就显 ...
- android学习资源
https://codelabs.developers.google.com/ https://developer.android.com/ http://v.youku.com/v_show/id_ ...
- spring beans 接口
- mac 安装opencv-python
Mac下安装opencv-python 项目中使用的是opencv 2的版本,因此下面说一下opencv2的一些安装流程. 安装方法 1:如果没有homebrew的话,需要先安装 安装命令: ruby ...
- U-boot for Tiny4412
我的开发板型号: Tiny4412ADK + S700 4GB Flash 1. Build uboot a) 安装好toolchain (arm-linux-gcc-4.5.1-v6-vfp-201 ...
- Linq查询满足条件记录集
通过linq查询datatable数据集合满足条件的数据集 1.首先定义查询字段的变量,比方深度 string strDepth=查询深度的值: var dataRows = from datarow ...
- Java第三次实验要求
实验三 类与对象(一) 一. 实验目的 1. 掌握类与对象的基本概念: 2. 掌握类的声明.创建与用法: 3. 掌握类的构造方法的定义与用法 4. 掌握类的成员变量.成员方法的定义与用法: 5. 理解 ...
- 百科知识 DMG文件如何打开
1 DMG格式是在MAC系统上的一个镜象文件,也可以说是压缩文件,如果你是使用苹果机或在PC上装了MAC OS X for X86,在MAC系统上双击这个文件就可以解开了:现在在Windows平台,我 ...
- java内存模型(Java Memory Model)
内容导航: Java内存模型 硬件存储体系结构 Java内存模型和硬件存储体系之间的桥梁: 共享对象的可见性 竞争条件 Java内存模型规定了JVM怎样与计算机存储系统(RAM)协调工作.JVM是一个 ...