12812022-03-29 19:41:45Valaki2Nemzetközi Rántott Hús Fesztiválcpp14Time limit exceeded 41/100596ms61444 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]++;
        }
    }
    int mini = INT_MAX, maxi = INT_MIN;
    for(int i = 1; i <= n; i++) {
        mini = min(mini, pref_all[i]);
        maxi = max(maxi, 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 = mini - 1;
    int r = maxi + 1;
    int range = r - l + 1;
    int offset = -l + 1;
    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
1Accepted2ms1868 KiB
2Time limit exceeded541ms1184 KiB
subtask27/7
3Accepted1ms1940 KiB
4Accepted1ms1932 KiB
subtask39/9
5Accepted2ms2212 KiB
6Accepted2ms2232 KiB
7Accepted3ms2212 KiB
subtask414/14
8Accepted291ms47884 KiB
9Accepted388ms50144 KiB
10Accepted365ms51040 KiB
11Accepted375ms50392 KiB
12Accepted152ms49940 KiB
13Accepted469ms61444 KiB
14Accepted143ms51876 KiB
15Accepted193ms52892 KiB
subtask511/11
16Accepted13ms8220 KiB
17Accepted21ms8196 KiB
18Accepted21ms8200 KiB
subtask60/29
19Time limit exceeded554ms7452 KiB
20Time limit exceeded596ms7520 KiB
21Time limit exceeded505ms7548 KiB
22Time limit exceeded592ms7620 KiB
subtask70/30
23Time limit exceeded577ms36148 KiB
24Time limit exceeded574ms37144 KiB
25Time limit exceeded573ms38128 KiB
26Time limit exceeded515ms39292 KiB
27Time limit exceeded560ms41672 KiB
28Time limit exceeded542ms43608 KiB
29Time limit exceeded574ms43176 KiB
30Time limit exceeded578ms36096 KiB
31Time limit exceeded588ms39136 KiB
32Time limit exceeded536ms47856 KiB
33Time limit exceeded560ms47616 KiB
34Time limit exceeded569ms47312 KiB
35Time limit exceeded561ms48552 KiB
36Accepted144ms56984 KiB
37Time limit exceeded503ms40148 KiB
38Accepted148ms56180 KiB
39Accepted158ms57168 KiB
40Time limit exceeded580ms49044 KiB