結果

問題 No.608 God's Maze
ユーザー nebukuro09
提出日時 2018-01-12 06:07:54
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 1,129 bytes
コンパイル時間 961 ms
コンパイル使用メモリ 115,712 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-12 23:30:25
合計ジャッジ時間 2,711 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;


int solve(int N, int X, bool[] s) {
    int left = N-1;
    int right = 0;
    int cnt = 0;
    foreach (i; 0..N) {
        if (s[i]) {
            left = min(left, i);
            right = max(right, i);
            cnt += 1;
        }
    }

    int ans = 0;
    int x = X;

    for (; x > left; --x) {
        cnt += s[x-1] ? -1 : 1;
        s[x-1] ^= 1;
        ans += 1;
    }

    for (; cnt > 0; ++x) {
        if (s[x]) {
            cnt += s[x+1] ? -1 : 1;
            s[x+1] ^= 1;
            cnt += s[x] ? -1 : 1;
            s[x] ^= 1;
            ans += 2;
        }
        if (cnt == 0) break;
        ans += 1;
        cnt += s[x+1] ? -1 : 1;
        s[x+1] ^= 1;
    }

    return ans;
}


void main() {
    auto X = readln.chomp.to!int - 1;
    auto S = readln.chomp.map!(c => c == '#').array;
    auto T = S.dup; T.reverse();
    auto N = S.length.to!int;


    min(solve(N, X, S), solve(N, N-X-1, T)).writeln;
}
0