結果

問題 No.608 God's Maze
ユーザー nanae
提出日時 2017-12-09 02:02:03
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 2,014 bytes
コンパイル時間 1,559 ms
コンパイル使用メモリ 148,204 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-12 22:56:10
合計ジャッジ時間 3,271 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

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


void main() {
    int n;
    string s;

    scan(n);
    scan(s);

    int l, r;

    foreach (i ; 0 .. s.length) {
        if (s[i] == '#') {
            l = i.to!int;
            break;
        }
    }

    foreach_reverse (i ; 0 .. s.length) {
        if (s[i] == '#') {
            r = i.to!int;
            break;
        }
    }

    n--;

    if (l == r && n == l) {
        writeln(3);
        return;
    }

    int ans;

    if (n <= l) {
        auto d = new int[](r + 1 - n);
        ans = l2r(s[n .. r + 1], d);
    }
    else if (r <= n) {
        auto d = new int[](n+1 - l);
        ans = l2r(s[l .. n+1].retro().to!string, d);
    }
    else {
        auto d1 = new int[](r + 1 - l);
        foreach (i ; 0 .. n - l) {
            d1[i]++;
        }
        ans = l2r(s[l .. r+1], d1);

        auto d2 = new int[](r + 1 - l);
        foreach (i ; 0 .. r - n) {
            d2[i]++;
        }

        debug {
            writeln(d2);
        }

        ans = min(ans, l2r(s[l .. r+1].retro.to!string, d2));
    }

    writeln(ans);
}

int l2r(string ss, int[] d) {
    auto s = new int[](ss.length);

    foreach (i ; 0 .. ss.length) {
        s[i] = (ss[i] == '#');
    }

    foreach (i ; 0 .. s.length - 1) {
        d[i+1]++;

        if (s[i] != (d[i] & 1)) {
            d[i]++;
            if (i + 1 < s.length - 1) {
                d[i+1]++;
            }
        }
    }

    debug {
        writeln(d);
    }

    return d.sum();
}


void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
0