結果

問題 No.2553 Holidays
ユーザー Yilin FeiYilin Fei
提出日時 2024-01-08 17:37:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 2,086 bytes
コンパイル時間 1,104 ms
コンパイル使用メモリ 84,612 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-01-08 17:37:04
合計ジャッジ時間 3,640 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    string s;
    cin >> s;

    int ans = 0, g2 = 0, g1 = 0;
    vector<int> d2, d1;

    // 分割字符串,并计数
    size_t last = 0, next = 0;
    while ((next = s.find("x", last)) != string::npos) {
        string seg = s.substr(last, next - last);
        ans += count(seg.begin(), seg.end(), 'o');
        vector<string> parts;
        size_t seg_last = 0, seg_next = 0;
        while ((seg_next = seg.find("o", seg_last)) != string::npos) {
            parts.push_back(seg.substr(seg_last, seg_next - seg_last));
            seg_last = seg_next + 1;
        }
        parts.push_back(seg.substr(seg_last));

        if (parts.size() == 1) {
            int l = parts[0].length();
            if (l) d1.push_back(l);
        } else {
            for (int l : {static_cast<int>(parts[0].length()), static_cast<int>(parts.back().length())}) {
                if (l % 2 == 0) {
                    g2 += l / 2;
                } else {
                    g2 += l / 2;
                    g1 += 1;
                }
            }
            for (int i = 1; i < parts.size() - 1; ++i) {
                int l = parts[i].length();
                if (l % 2 == 0) {
                    g2 += l / 2;
                } else {
                    d2.push_back(l);
                }
            }
        }
        last = next + 1;
    }

    sort(d2.begin(), d2.end());
    sort(d1.rbegin(), d1.rend());

    for (int l : d2) {
        if (m >= l / 2) {
            ans += l;
            m -= l / 2;
        } else {
            ans += 2 * m;
            m = 0;
        }
    }

    int t = min(m, g2);
    ans += 2 * t;
    m -= t;

    for (int l : d1) {
        if (!m) break;
        ans += 1;
        m--;
        l--;
        while (m && l >= 2) {
            m--;
            ans += 2;
            l -= 2;
        }
        g1 += l;
    }

    t = min(m, g1);
    ans += t;
    m -= t;

    cout << ans << endl;

    return 0;
}
0