Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister
来源:互联网 发布:南京纬创软件 编辑:程序博客网 时间:2024/06/08 02:35
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 hasm 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 withn rows and m + 2 columns, where the first and the last columns represent the stairs, and them 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 andm (1 ≤ n ≤ 15 and1 ≤ 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 lengthm + 2 representing a floor (the left stairs, thenm 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 always0.
Print a single integer — the minimum total time needed to turn off all the lights.
2 200100100
5
3 4001000000010000010
12
4 301110011100111001110
18
In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room2 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,表示灯亮。往左往右需要一分钟,上楼下楼需要一分钟,关灯不需要时间。问关完全部的灯需要多少时间。
题解:由于起点是知道的,终点不确定,那么再输入的时候判断终点,然后从终点往起点推。(代码注释会更清楚点……)
ac code:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cstdlib>#include <cmath>#include <vector>#include <list>#include <deque>#include <queue>#include <iterator>#include <stack>#include <map>#include <set>#include <algorithm>#include <cctype>using namespace std;#define si1(a) scanf("%d",&a)#define si2(a,b) scanf("%d%d",&a,&b)#define sd1(a) scanf("%lf",&a)#define sd2(a,b) scanf("%lf%lf",&a,&b)#define ss1(s) scanf("%s",s)#define pi1(a) printf("%d\n",a)#define pi2(a,b) printf("%d %d\n",a,b)#define mset(a,b) memset(a,b,sizeof(a))#define forb(i,a,b) for(int i=a;i<b;i++)#define ford(i,a,b) for(int i=a;i<=b;i++)typedef long long LL;const int N=1100001;const int M=6666666;const int INF=0x3f3f3f3f;const double PI=acos(-1.0);const double eps=1e-7;char pla[105];int main(){ int n,m; si2(n,m); int flag=0;//判断是否有灯 int L,R,cnt;//L在左边的楼梯口,R在右边的楼梯口 for(int i=0;i<n;i++) { int l=m+1,r=0;//记录最左边,最右边的灯 scanf("%s",pla); for(int j=0;j<strlen(pla);j++) { if(pla[j]=='1') { if(l==m+1) l=j; r=j; } } cnt=min(L+2*r+1,R+m+2);//回到最左边需要最少的时间 R=min(L+m+2,R+2*(m-l)+3);//回到最右边需要最少的时间 L=cnt; if(r&&!flag) { L=r;flag=1;R=m-l+1;//最高层的时间,也就是起始时间 } } printf("%d\n",flag?L:0); return 0;}
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister dfs深度优先搜索
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister(动态规划)
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister —— DP
- Codeforces Round #417 (Div. 2)Sagheer, the Hausmeister 乱搞 Sagheer and Nubian Market 二分
- Codeforces 812B-Sagheer, the Hausmeister
- Codeforces 812B Sagheer, the Hausmeister[dp]
- codeforces 812 B Sagheer, the Hausmeister(枚举)
- Codeforces 812 B Sagheer, the Hausmeister
- codeforces 812B Sagheer, the Hausmeister dp
- codeforces 812b Sagheer, the Hausmeister
- codeforces Sagheer, the Hausmeister 812B
- Codeforces 812B-Sagheer, the Hausmeister
- Sagheer, the Hausmeister CodeForces
- Sagheer, the Hausmeister CodeForces
- B. Sagheer, the Hausmeister
- leetcode 452. Minimum Number of Arrows to Burst Balloons
- spacemacs操作卡顿的解决方法
- 应用JavaScript(一):表单验证
- 搭建自己的Nuget服务器
- TCP三次握手与四次挥手
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister
- LCS求最长公共子串
- Linux权限
- web安全色
- Babelfish POJ
- 单变量线性回归中的梯度下降法求解代价函数的最小值
- Linux下安装Mongodb
- LeetCode
- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter