結果

問題 No.1437 01 Sort
ユーザー novaandcabralnovaandcabral
提出日時 2024-04-22 14:39:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,229 bytes
コンパイル時間 1,969 ms
コンパイル使用メモリ 174,928 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-22 14:39:57
合計ジャッジ時間 3,393 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 WA -
testcase_06 AC 2 ms
5,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 2 ms
5,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

unsigned long long solve(int N, string S) {
    if (vector<char>(S.begin(), S.end()) == vector<char>(S.begin(), S.end())) {
        return 0;
    }

    string S2 = S + S;

    // 计算 S[l:r] 中 '0' 的数量,zero_cnt[r] - zero_cnt[l]
    vector<int> zero_cnt(S2.length() + 1, 0);
    vector<int> one_cnt(S2.length() + 1, 0);
    for (char c : S2) {
        zero_cnt.push_back(zero_cnt.back() + (c == '0'));
        one_cnt.push_back(one_cnt.back() + (c == '1'));
    }

    unordered_map<string, int> cache;

    function<int(int, int, int)> calc = [&](int l, int t, int r) -> int {
        int left0 = zero_cnt[t + 1] - zero_cnt[l];
        int right1 = one_cnt[r + 1] - one_cnt[t + 1];
        int end = t + right1;
        int rem = (N - 1 - end) % N;
        assert(l <= t && t <= end && end <= r);
        if (end < N) {
            if (right1) {
                int rskip = (one_cnt[N + 1] - one_cnt[t + 1]) ? 1 : 0;
                right1 = right1 + 1 - rskip;
            }
            return max(left0, right1) * N + rem;
        } else if (t < N) {
            int rskip = (one_cnt[N + 1] - one_cnt[t + 1]) ? 1 : 0;
            return max(left0 - 1, right1 - rskip) * N + rem;
        } else {
            int lskip = (zero_cnt[N + 1] - zero_cnt[l + 1]) ? 1 : 0;
            return max(left0 - lskip, right1) * N + rem;
        }
    };

    vector<int> ones;
    for (int i = 0; i < S2.length(); i++) {
        if (S2[i] == '1') {
            ones.push_back(i);
        }
    }

    unsigned long long answer = 1e18;
    int t = -1;
    for (int i = 0; i < one_cnt[N]; i++) {
        int l = ones[i];
        int r = ones[i + one_cnt[N] - 1];
        t = max(l, t);
        
            int c = calc(l, t, r);
        while (true) {
            int nt = t + 1;
            while (nt <= r && c == calc(l, nt, r)) {
                nt++;
            }
            if (nt == r + 1 || c < calc(l, nt, r)) {
                break;
            }
            t = nt;
        }
        answer = min(answer, (unsigned long long)c);
    }
    return answer;
}

int main() {
    int N;
    cin >> N;
    string S;
    cin >> S;
    cout << solve(N, S) << endl;
    return 0;
}
0