12802022-03-29 19:38:35Valaki2Nemzetközi Rántott Hús Fesztiválcpp14Time limit exceeded 27/100597ms120444 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second

const int inf = 1e8 + 5;

int val_between(int l, int r, vector<int> &pref_H) {
    return pref_H[r] - pref_H[l - 1];
}

void update(int v, int vl, int vr, int pos, int val, vector<int> &segtree) {
    if(vl == vr) {
        segtree[v] = val;
    } else {
        int mid = (vl + vr) / 2;
        if(pos <= mid) {
            update(2 * v, vl, mid, pos, val, segtree);
        } else {
            update(2 * v + 1, mid + 1, vr, pos, val, segtree);
        }
        segtree[v] = min(segtree[2 * v], segtree[2 * v + 1]);
    }
}

int query(int v, int vl, int vr, int ql, int qr, vector<int> &segtree) {
    if(vl > qr || vr < ql) {
        return inf;
    }
    if(vl == ql && vr == qr) {
        return segtree[v];
    }
    int mid = (vl + vr) / 2;
    return min(query(2 * v, vl, mid, ql, min(qr, mid), segtree),
                query(2 * v + 1, mid + 1, vr, max(ql, mid + 1), qr, segtree));
}

vector<int> try_case(int n, string s) {
    vector<int> pref_K(1 + n, 0);
    vector<int> pref_H(1 + n, 0);
    vector<int> pref_all(1 + n, 0);
    for(int i = 1; i <= n; i++) {
        pref_K[i] += pref_K[i - 1];
        pref_H[i] += pref_H[i - 1];
        pref_all[i] += pref_all[i - 1];
        if(s[i] == 'H') {
            pref_H[i]++;
            pref_all[i]--;
        } else {
            pref_K[i]++;
            pref_all[i]++;
        }
    }
    // pref[r + 1] - pref[i - 1] < 0
    // pref[r + 1] < pref[i - 1]
    // segtree: l=-(1e6+2), r=(1e6+2)
    // range=2e6+5
    // size=1+4*range=4*(2e6+5)
    // optimization: l-r for min-max pref value?
    int l = -(n + 2);
    int r = n + 2;
    int range = r - l + 1;
    int offset = -l + 3;
    vector<int> segtree(1 + 4 * range, inf);
    vector<int> result(1 + n, 0);
    for(int i = n; i >= 1; i--) {
        update(1, l + offset, r + offset, pref_all[i] + offset, i, segtree);
        int cur_rp1 = query(1, l + offset, r + offset, l + offset, pref_all[i - 1] - 1 + offset, segtree);
        if(cur_rp1 == inf) {
            cur_rp1 = n + 1;
        }
        int cur_r = cur_rp1 - 1;
        if(cur_r >= i) {
            result[i] = val_between(i, cur_r, pref_H);
        }
    }
    /*cout << s.substr(1, (int) s.size() - 1) << "\n";
    for(int i = 1; i <= n; i++) {
        cout << result[i] << " ";
    }
    cout << "\n";*/
    return result;
}

void solve() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    s = " " + s;
    string t = s;
    for(int i = 1; i <= n; i++) {
        if(t[i] == 'M') {
            t[i] = 'H';
        }
    }
    vector<int> ans(1 + n, 0);
    int idx = 0;
    do {
        vector<int> cur = try_case(n, t);
        for(int i = 1; i <= n; i++) {
            ans[i] = max(ans[i], cur[i]);
        }
        idx++;
        while(idx <= n && s[idx] != 'M') {
            idx++;
        }
        if(idx <= n) {
            t[idx] = 'K';
        }
    } while(idx <= n);
    for(int i = 1; i <= n; i++) {
        cout << ans[i] << " ";
    }
    cout << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted2ms1832 KiB
2Time limit exceeded587ms1408 KiB
subtask27/7
3Accepted1ms1860 KiB
4Accepted1ms1868 KiB
subtask39/9
5Accepted4ms2244 KiB
6Accepted3ms2260 KiB
7Accepted3ms2268 KiB
subtask40/14
8Time limit exceeded526ms55768 KiB
9Time limit exceeded527ms111228 KiB
10Time limit exceeded504ms57720 KiB
11Time limit exceeded533ms113188 KiB
12Accepted442ms114180 KiB
13Time limit exceeded540ms60608 KiB
14Accepted430ms114588 KiB
15Accepted474ms114244 KiB
subtask511/11
16Accepted17ms6912 KiB
17Accepted35ms6916 KiB
18Accepted26ms6968 KiB
subtask60/29
19Time limit exceeded528ms6324 KiB
20Time limit exceeded541ms6328 KiB
21Time limit exceeded597ms6324 KiB
22Time limit exceeded524ms6272 KiB
subtask70/30
23Time limit exceeded537ms60788 KiB
24Time limit exceeded518ms58416 KiB
25Time limit exceeded509ms59428 KiB
26Time limit exceeded523ms60324 KiB
27Time limit exceeded503ms115820 KiB
28Time limit exceeded512ms59304 KiB
29Time limit exceeded513ms59216 KiB
30Time limit exceeded554ms60204 KiB
31Time limit exceeded538ms61168 KiB
32Time limit exceeded561ms59204 KiB
33Time limit exceeded582ms60056 KiB
34Time limit exceeded574ms61044 KiB
35Time limit exceeded582ms61964 KiB
36Accepted451ms117484 KiB
37Time limit exceeded558ms63992 KiB
38Accepted435ms119460 KiB
39Accepted453ms120444 KiB
40Time limit exceeded577ms66876 KiB