码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces - 812B Sagheer, the Hausmeister

时间:2018-03-17 23:27:53      阅读:417      评论:0      收藏:0      [点我收藏+]

标签:pos   from   ios   min   cout   int   break   namespace   lam   

题意:给你

 

 

#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

标签:pos   from   ios   min   cout   int   break   namespace   lam   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!