結果

問題 No.2553 Holidays
ユーザー InTheBloomInTheBloom
提出日時 2023-11-20 23:37:10
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 3,729 bytes
コンパイル時間 1,525 ms
コンパイル使用メモリ 176,256 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-11-25 12:30:35
合計ジャッジ時間 2,910 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
6,676 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 3 ms
6,676 KB
testcase_05 WA -
testcase_06 AC 2 ms
6,676 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 5 ms
6,676 KB
testcase_12 AC 4 ms
6,676 KB
testcase_13 WA -
testcase_14 AC 5 ms
6,676 KB
testcase_15 AC 3 ms
6,676 KB
testcase_16 AC 5 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 4 ms
6,676 KB
testcase_22 AC 3 ms
6,676 KB
testcase_23 WA -
testcase_24 AC 4 ms
6,676 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 1 ms
6,676 KB
testcase_31 AC 1 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 WA -
testcase_34 AC 1 ms
6,676 KB
testcase_35 AC 1 ms
6,676 KB
testcase_36 AC 1 ms
6,676 KB
testcase_37 AC 2 ms
6,676 KB
testcase_38 AC 1 ms
6,676 KB
testcase_39 AC 1 ms
6,676 KB
testcase_40 AC 1 ms
6,676 KB
testcase_41 AC 1 ms
6,676 KB
testcase_42 AC 1 ms
6,676 KB
testcase_43 AC 2 ms
6,676 KB
testcase_44 AC 1 ms
6,676 KB
testcase_45 AC 1 ms
6,676 KB
testcase_46 AC 1 ms
6,676 KB
testcase_47 AC 1 ms
6,676 KB
testcase_48 AC 1 ms
6,676 KB
testcase_49 AC 1 ms
6,676 KB
testcase_50 AC 2 ms
6,676 KB
testcase_51 AC 1 ms
6,676 KB
testcase_52 AC 1 ms
6,676 KB
testcase_53 AC 1 ms
6,676 KB
testcase_54 AC 1 ms
6,676 KB
testcase_55 AC 2 ms
6,676 KB
testcase_56 AC 1 ms
6,676 KB
testcase_57 AC 1 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    int N, M; readln.read(N, M);
    string S = readln.chomp;

    solve(N, M, S);
}

void solve (int N, int M, string S) {
    /*
       'x'や'o'のあるところで区切って良い。本質的に、区切られた文字は3種類のみ
       1. x --- x
       2. x --- o
       3. o --- o
       このうち、最も優先すべきはタイプ3で、かつ-の長さが奇数のもの。これは最高効率で埋めることができる可能性がある。
       次に優先度が高いのが長さ2以上の2と、3かつ-の長さが偶数のもの。これらは確実に1個で2つの祝日を生み出せる。
       次に優先度が高いのが長さ3以上の1
       一番優先度が低いのが長さ2以下の1と長さ1の2

       これらを守って処理すれば必ず最高効率になるはず
     */

    /* 区間分割 */
    auto T = new BinaryHeap!(Array!int, "a<b")[](4);
    int l = 0;
    int r = 0;

    while (true) {
        if (S.length <= l) break;
        if (S[l] == '-') {
            int type = 0;
            r = l;
            if (0 < r && S[r-1] == 'o') type++;
            while (true) {
                if (r == S.length || S[r] == 'x') {
                    break;
                }
                if (S[r] == 'o') {
                    type++;
                    break;
                }
                r++;
            }
            if (type == 2 && (r-l) % 2 == 1) T[type+1].insert(r-l);
            else T[type].insert(r-l);
            l = r;
        }
        l++;
    }

    int ans = 0;
    foreach (c; S) if (c == 'o') ans++;

    /* 処理 */
    while (0 < M || !T[3].empty) {
        // dbg
        /*
        foreach (t; T) writeln(t.dup);
        writeln("ans: ", ans);
        writeln("M: ", M);
        writeln("");
        */

        if (!T[3].empty) {
            auto head = T[3].front; T[3].removeFront;
            if (head == 1) { ans++; continue; }
            ans += min(head, 2*(M+1));
            M -= min(head/2, M);
            continue;
        }

        if (!T[2].empty) {
            auto head = T[2].front; T[2].removeFront;
            ans += min(head, 2*M);
            M -= min(head/2, M);
            continue;
        }

        if (!T[1].empty && 2 <= T[1].front) {
            auto head = T[1].front; T[1].removeFront;
            if (head % 2 == 0) {
                ans += min(head, 2*M);
                M -= min(head/2, M);
            }
            else {
                ans += min(head-1, 2*M);
                M -= min(head/2, M);
                head -= min(head-1, 2*M);
                T[1].insert(head);
            }
            continue;
        }

        if (!T[0].empty && 3 <= T[0].front) {
            auto head = T[0].front; T[0].removeFront;
            ans++;
            M--;
            T[1].insert(head-1);
            continue;
        }

        /* 優先度最悪 */
        if (!T[0].empty && T[0].front <= 2) {
            if (T[0].front <= M) {
                ans += T[0].front;
                M -= T[0].front;
                T[0].removeFront;
            }
            else {
                ans += M;
                auto head = T[0].front - M;
                T[0].removeFront;
                T[1].insert(head);
                M = 0;
            }
            continue;
        }
        if (!T[1].empty && T[1].front == 1) {
            T[1].empty;
            ans++;
            M--;
            continue;
        }

        /* 選択できるやつがもうない */
        break;
    }

    writeln(ans);
}

void read(T...)(string S, ref T args) {
    auto buf = S.split;
    foreach (i, ref arg; args) {
        arg = buf[i].to!(typeof(arg));
    }
}
0