100672024-03-26 09:21:17szilNemzetközi Rántott Hús Fesztiválcpp17Accepted 100/100109ms14408 KiB
#include <bits/stdc++.h>

using ll = long long;
using namespace std;

const int MAXN = 1'000'001;

struct State {
    int res = 0, extra = 0, upgraded = 0;
};

State operator+(State a, State b) {
    return {a.res + b.res, a.extra + b.extra, a.upgraded + b.upgraded};
}

char s[MAXN];
int ans[MAXN];

void solve() {
    int n; cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> s[i];
    }
    
    stack<State> blocked;
    State curr;
    for (int i = n; i >= 1; i--) {
        if (s[i] == 'H') {
            blocked.push(curr);
            curr = State();
        } else  {
            bool used = true;
            if (blocked.empty()) {
                if (curr.upgraded) {
                    curr.upgraded--;
                    curr.res++;
                } else if (curr.extra) {
                    curr.extra--;
                    curr.upgraded++;
                } else {
                    used = false;
                }
                
            } else {
                curr = curr + blocked.top(); 
                curr.res++;
                blocked.pop();
            }
            if (s[i] == 'M') {
                if (used) curr.extra++;
                else curr.upgraded++;
            }
        }

        ans[i] = curr.res;
    }

    for (int i = 1; i <= n; i++) {
        cout << ans[i] << " ";
    }
    cout << "\n";
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1836 KiB
2Accepted3ms2188 KiB
subtask27/7
3Accepted2ms2112 KiB
4Accepted3ms2244 KiB
subtask39/9
5Accepted3ms2484 KiB
6Accepted3ms2476 KiB
7Accepted3ms2744 KiB
subtask414/14
8Accepted108ms12608 KiB
9Accepted109ms12748 KiB
10Accepted108ms12884 KiB
11Accepted109ms13104 KiB
12Accepted100ms13228 KiB
13Accepted105ms13448 KiB
14Accepted101ms13640 KiB
15Accepted100ms13696 KiB
subtask511/11
16Accepted3ms3884 KiB
17Accepted3ms4008 KiB
18Accepted3ms4084 KiB
subtask629/29
19Accepted3ms4108 KiB
20Accepted3ms4160 KiB
21Accepted3ms4152 KiB
22Accepted3ms4112 KiB
subtask730/30
23Accepted108ms13944 KiB
24Accepted108ms13888 KiB
25Accepted108ms13992 KiB
26Accepted108ms13916 KiB
27Accepted107ms13932 KiB
28Accepted107ms13916 KiB
29Accepted105ms13844 KiB
30Accepted107ms13844 KiB
31Accepted107ms13836 KiB
32Accepted105ms14052 KiB
33Accepted104ms14036 KiB
34Accepted105ms14060 KiB
35Accepted104ms14160 KiB
36Accepted101ms14300 KiB
37Accepted104ms14408 KiB
38Accepted100ms14212 KiB
39Accepted100ms14228 KiB
40Accepted104ms14268 KiB