zoukankan      html  css  js  c++  java
  • Codefroces 812 B. Sagheer, the Hausmeister

    B. Sagheer, the Hausmeister
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    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.

    Input

    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.

    Output

    Print a single integer — the minimum total time needed to turn off all the lights.

    Examples
    Input
    2 2
    0010
    0100
    Output
    5
    Input
    3 4
    001000
    000010
    000010
    Output
    12
    Input
    4 3
    01110
    01110
    01110
    01110
    Output
    18
    Note

    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.

    贪心+模拟

    #include <iostream> 
    #include <algorithm> 
    #include <cstring> 
    #include <cstdio>
    #include <vector> 
    #include <queue> 
    #include <cstdlib> 
    #include <iomanip>
    #include <cmath> 
    #include <ctime> 
    #include <map> 
    #include <set> 
    using namespace std; 
    #define lowbit(x) (x&(-x)) 
    #define max(x,y) (x>y?x:y) 
    #define min(x,y) (x<y?x:y) 
    #define MAX 100000000000000000 
    #define MOD 1000000007
    #define pi acos(-1.0) 
    #define ei exp(1) 
    #define PI 3.141592653589793238462
    #define ios() ios::sync_with_stdio(false)
    #define INF 0x3f3f3f3f3f 
    #define mem(a) (memset(a,0,sizeof(a))) 
    typedef long long ll;
    char a[105][1005];
    int n,m,ans,pos,f[105][2],pl=0,pr=MOD;
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%s",a[n-i+1]+1);
        int k=1;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m+2;j++)
                if(a[i][j]=='1') k=i;
        for(int i=1;i<=k;i++)
        {
            int l=1,r=m+2;
            for(int j=1;j<=m+2;j++) if(a[i][j]=='1') l=j;
            for(int j=m+1;j>=1;j--) if(a[i][j]=='1') r=j;
            ans=pl,pos=pr;
            if(i<k) 
            {
                pl=min(ans+(l-1)*2,pos+m+1);//返回加一路走到黑
                pr=min(pos+(m+2-r)*2,ans+m+1);
            }
            else
            {
                pl+=l-1;
                pr+=m+2-r;
            }
        }
        printf("%d
    ",min(pl,pr)+k-1);
        return 0;
    }
  • 相关阅读:
    Oracle中的4大空值处理函数用法举例
    PyCharm安装
    Python安装与环境变量的配置
    多层分组排序问题
    将时间点的数据变成时间段的数据
    根据状态变化情况,求最大值和最小值
    ubuntu 源码安装 swig
    CSDN博客排名第一名,何许人也
    thinkPHP的常用配置项
    拔一拔 ExtJS 3.4 里你遇到的没遇到的 BUG(1)
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7264312.html
Copyright © 2011-2022 走看看