12822022-03-29 19:46:28Valaki2Nemzetközi Rántott Hús Fesztiválcpp14Wrong answer 0/100593ms68804 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
1Wrong answer2ms1860 KiB
2Wrong answer107ms2200 KiB
subtask20/7
3Wrong answer1ms1908 KiB
4Wrong answer1ms1916 KiB
subtask30/9
5Wrong answer2ms2176 KiB
6Wrong answer2ms2200 KiB
7Wrong answer2ms2204 KiB
subtask40/14
8Wrong answer126ms47908 KiB
9Wrong answer123ms50184 KiB
10Wrong answer115ms51040 KiB
11Wrong answer112ms52028 KiB
12Wrong answer116ms51572 KiB
13Wrong answer119ms63084 KiB
14Wrong answer104ms53548 KiB
15Wrong answer100ms54528 KiB
subtask50/11
16Wrong answer2ms9832 KiB
17Wrong answer2ms9832 KiB
18Wrong answer3ms9836 KiB
subtask60/29
19Wrong answer129ms10148 KiB
20Wrong answer134ms10164 KiB
21Wrong answer263ms10188 KiB
22Wrong answer314ms10196 KiB
subtask70/30
23Time limit exceeded541ms37700 KiB
24Time limit exceeded558ms38592 KiB
25Time limit exceeded574ms39632 KiB
26Time limit exceeded556ms40508 KiB
27Time limit exceeded580ms42984 KiB
28Time limit exceeded593ms45068 KiB
29Time limit exceeded560ms44124 KiB
30Time limit exceeded560ms37072 KiB
31Time limit exceeded565ms40036 KiB
32Time limit exceeded561ms46152 KiB
33Time limit exceeded568ms48324 KiB
34Time limit exceeded570ms50436 KiB
35Time limit exceeded555ms48864 KiB
36Wrong answer104ms57292 KiB
37Wrong answer104ms68804 KiB
38Wrong answer123ms59260 KiB
39Wrong answer114ms60244 KiB
40Time limit exceeded536ms49244 KiB