100532024-03-25 20:27:14111Nemzetközi Rántott Hús Fesztiválcpp17Time limit exceeded 70/100551ms364788 KiB
#pragma GCC optimize("Ofast,unroll-loops")

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

// #define int long long

char bf[11000000];
int bfp=0;

void wc(char c) {
	bf[bfp++] = c;
}

void wu(int i) {
	int x = 1;
	int c = 1;
	while (x <= i / 10) {
		x *= 10;
		c++;
	}
	while (c--) {
		int d = i / x;
		wc('0' + d);
		i -= d * x;
		i *= 10;
	}
}

alignas(64) int tk[20][1000001],th[20][1000001];

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
	cin>>N;
	string S;
	cin>>S;
	vector<int>c(N+1),vk(N+1),vh(N+1),w(N+1),f(1);
	for(int i=0;i<N;i++){
		if(S[i]=='M'){
			f.push_back(i);
		}
		c[i+1]=c[i]+(S[i]!='K');
		vk[i+1]=(S[i]!='H'?vk[i]+1:vk[i]-1);
		vh[i+1]=(S[i]!='K'?vh[i]-1:vh[i]+1);
		w[i+1]=w[i]+(S[i]=='M');
	}
	f.push_back(N);
	for(int i=0;i<N+1;i++){
		tk[0][i]=vk[i];
		th[0][i]=vh[i];
	}
	for(int j=1;j<20;j++){
		for(int l=0,r=1<<j-1;r<N+1;l++,r++){
			tk[j][l]=min(tk[j-1][l],tk[j-1][r]);
		}
	}
	for(int j=1;j<20;j++){
		for(int l=0,r=1<<j-1;r<N+1;l++,r++){
			th[j][l]=min(th[j-1][l],th[j-1][r]);
		}
	}
	auto qk=[&](int l,int r)->int{
		int i=__lg(r-l);
		return min(tk[i][l],tk[i][r-(1<<i)]);
	};
	auto qh=[&](int l,int r)->int{
		int i=__lg(r-l);
		return min(th[i][l],th[i][r-(1<<i)]);
	};
	for(int i=0;i<N;i++){
		int b=qk(i+1,N+1)-vk[i]>=0;
		if(b){
			int l=1,h=w[N]-w[i]+1;
			while(l<h){
				int m=(l+h)/2;
				int j=f[w[i]+m];
				int x=vk[j]-vk[i];
				if(qh(j,N+1)-vh[j]+x>=0){
					h=m;
				}
				else{
					l=m+1;
				}
			}
			wu(c[N]-c[i]-(l-1));wc(' ');
		}
		else{
			int l=i+1,h=N;
			while(l<h){
				int m=(l+h)/2;
				if(qk(i+1,m+1)-vk[i]>=0){
					l=m+1;
				}
				else{
					h=m;
				}
			}
			wu((l-1-i)/2);wc(' ');
		}
	}
	wc('\n');
	fwrite(bf,sizeof(char),bfp,stdout);
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2072 KiB
2Accepted4ms3700 KiB
subtask27/7
3Accepted3ms2484 KiB
4Accepted3ms2692 KiB
subtask39/9
5Accepted4ms4360 KiB
6Accepted4ms4428 KiB
7Accepted4ms4656 KiB
subtask414/14
8Accepted377ms345720 KiB
9Accepted354ms346368 KiB
10Accepted356ms346340 KiB
11Accepted360ms346296 KiB
12Accepted287ms350000 KiB
13Accepted261ms351444 KiB
14Accepted324ms347540 KiB
15Accepted370ms346828 KiB
subtask511/11
16Accepted3ms3640 KiB
17Accepted3ms3860 KiB
18Accepted3ms3804 KiB
subtask629/29
19Accepted4ms5204 KiB
20Accepted4ms5208 KiB
21Accepted4ms5344 KiB
22Accepted4ms5300 KiB
subtask70/30
23Accepted456ms353768 KiB
24Accepted458ms353948 KiB
25Time limit exceeded546ms354172 KiB
26Accepted472ms357108 KiB
27Time limit exceeded549ms358796 KiB
28Accepted458ms359604 KiB
29Accepted444ms360576 KiB
30Time limit exceeded551ms357024 KiB
31Accepted460ms358688 KiB
32Accepted432ms362124 KiB
33Accepted490ms363352 KiB
34Accepted368ms364252 KiB
35Accepted375ms364788 KiB
36Accepted291ms351236 KiB
37Accepted293ms353216 KiB
38Accepted319ms349116 KiB
39Accepted370ms348376 KiB
40Accepted368ms364316 KiB