首页 > 其他 > 详细

CodeForces - 812B Sagheer, the Hausmeister

时间:2018-03-17 23:19:07      阅读:319      评论:0      收藏:0      [点我收藏+]

题意:给你

 

 

#define  _CRT_SECURE_NO_WARNINGS
#include<cstdio>  
#include<algorithm>  
#include<iostream>
#include<string>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
int m, n,len;
int dp[20][2];//清完i层回到最左0右1 所需的min
int far[20][2];//每层楼离左0右1 最远的灯
int no[20];
int main() {
    while (cin >> n >> m) {
        for (int i = 1; i <= n; i++) {//从顶层开始输入
            string s;
            cin >> s;
            len = s.length();
            int j;
            int ok = 0;
            for (j = 1; j < len - 1; j++) if (s[j] == 1) { far[n + 1 - i][1] = len - j; break; }

            for (j = len - 1; j > 0; j--)if (s[j] == 1) { far[n + 1 - i][0] = j + 1; break; }
            for (j = 0; j < len; j++) { if (s[j] == 1) ok = 1; }
            if (ok == 0)far[n + 1 - i][0] = 1, far[n + 1 - i][1] = 1, no[n + 1 - i] = 1;
        }
        //如果某层没灯+1跳过:
        if (no[1]) far[1][0] = 1;//floor 1 no lamp
        dp[1][1] = len - 1; dp[1][0] = far[1][0] * 2 - 2;
        for (int i = 2; i <= n; i++) {
            for (int j = 0; j <= 1; j++)dp[i][j] = maxn;
            dp[i][0] = min(max(0, dp[i - 1][0] + far[i][0] * 2 - 1), dp[i - 1][1] + len);
            dp[i][1] = min(max(0, dp[i - 1][1] + far[i][1] * 2 - 1), dp[i - 1][0] + len);
        }
        /*else {
            for (int j = 0; j <= 1; j++)dp[i][j] = maxn;
            dp[i][0] = min(dp[i - 1][0] + 1, dp[i - 1][1] + len);
            dp[i][1]= min(dp[i - 1][1] + 1, dp[i - 1][0] + len);
        }*/
        //1st floor start from left,
        int k = n;
        while (no[k])k--;
        
        int ans = min(dp[k - 1][0] + far[k][0], dp[k - 1][1] + far[k][1]);
        //1层
        if (k == 1) {
            far[1][0]--; ans = far[1][0];
        }
        cout << ans << endl;
    }
    cin >> n;
}

 

CodeForces - 812B Sagheer, the Hausmeister

原文:https://www.cnblogs.com/SuuT/p/8593136.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!