100602024-03-25 20:47:48111Nemzetközi Rántott Hús Fesztiválcpp17Accepted 100/100465ms362888 KiB
#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;
	}
}

#define MN 1000002

alignas(64) int c[MN],vk[MN],vh[MN],w[MN],f[MN],fe,tk[20][MN],th[20][MN];

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
	cin>>N;
	string S;
	cin>>S;
	f[fe++]=1;
	for(int i=0;i<N;i++){
		if(S[i]=='M'){
			f[fe++]=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[fe++]=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]);
			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
1Accepted3ms2024 KiB
2Accepted4ms3680 KiB
subtask27/7
3Accepted3ms2336 KiB
4Accepted3ms2564 KiB
subtask39/9
5Accepted4ms4068 KiB
6Accepted4ms4284 KiB
7Accepted4ms4424 KiB
subtask414/14
8Accepted379ms345504 KiB
9Accepted358ms346464 KiB
10Accepted358ms346420 KiB
11Accepted360ms346120 KiB
12Accepted326ms350016 KiB
13Accepted270ms351568 KiB
14Accepted324ms347464 KiB
15Accepted375ms346940 KiB
subtask511/11
16Accepted3ms3800 KiB
17Accepted3ms3748 KiB
18Accepted3ms3872 KiB
subtask629/29
19Accepted4ms5200 KiB
20Accepted4ms5188 KiB
21Accepted4ms5200 KiB
22Accepted4ms5344 KiB
subtask730/30
23Accepted458ms353000 KiB
24Accepted462ms353008 KiB
25Accepted458ms352988 KiB
26Accepted465ms355756 KiB
27Accepted460ms357324 KiB
28Accepted458ms358332 KiB
29Accepted451ms358944 KiB
30Accepted460ms355732 KiB
31Accepted458ms357164 KiB
32Accepted435ms360548 KiB
33Accepted409ms361700 KiB
34Accepted361ms362576 KiB
35Accepted340ms362664 KiB
36Accepted293ms350360 KiB
37Accepted266ms351948 KiB
38Accepted324ms347880 KiB
39Accepted409ms347516 KiB
40Accepted375ms362888 KiB