100462024-03-25 19:51:33111Nemzetközi Rántott Hús Fesztiválcpp17Runtime error 56/100237ms523592 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
	cin>>N;
	string S;
	cin>>S;
	vector<int>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);
		}
		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');
	}
	vector<array<int,20>>tk(N+1),th(N+1);
	for(int i=0;i<N+1;i++){
		tk[i][0]=vk[i];
		th[i][0]=vh[i];
	}
	for(int j=1;j<20;j++){
		for(int l=0,r=1<<j-1;r<N+1;l++,r++){
			tk[l][j]=min(tk[l][j-1],tk[r][j-1]);
			th[l][j]=min(th[l][j-1],th[r][j-1]);
		}
	}
	auto qk=[&](int l,int r)->int{
		int i=__lg(r-l);
		return min(tk[l][i],tk[r-(1<<i)][i]);
	};
	auto qh=[&](int l,int r)->int{
		int i=__lg(r-l);
		return min(th[l][i],th[r-(1<<i)][i]);
	};
	for(int i=0;i<N;i++){
		int b=qk(i+1,N+1)-vk[i]>=0;
		if(b){
			int a=0;
			int c=0;
			int d=0;
			for(int j=i;j<N;j++){
				char C=S[j];
				if(C=='H'){
					if(c==0){
						if(d==0){
							break;
						}
						else{
							c++;
							d--;
						}
					}
					else{
						c--;
						a++;
					}
				}
				if(C=='K'){
					c++;
				}
				if(C=='M'){
					if(c==0){
						c++;
					}
					else{
						a++;
						c--;
						d++;
					}
				}
			}
			cout<<a<<' ';
		}
		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;
				}
			}
			cout<<(l-1-i)/2<<' ';
		}
		// int canFinish=1;
		// {
			// int c=0;
			// for(int j=i;j<N;j++){
				// char C=S[j];
				// if(C=='M')C='K';
				// if(C=='H'){
					// if(c==0){
						// canFinish=0;
					// }
					// else{
						// c--;
					// }
				// }
				// if(C=='K'){
					// c++;
				// }
			// }
		// }
		// if(canFinish!=b)exit(1);
	}
	cout<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1956 KiB
2Accepted48ms5560 KiB
subtask27/7
3Accepted3ms2276 KiB
4Accepted3ms2492 KiB
subtask39/9
5Accepted8ms5852 KiB
6Accepted4ms6148 KiB
7Accepted32ms6460 KiB
subtask40/14
8Runtime error231ms523592 KiB
9Runtime error189ms523564 KiB
10Runtime error189ms523544 KiB
11Runtime error232ms523508 KiB
12Runtime error221ms523488 KiB
13Runtime error175ms523252 KiB
14Runtime error222ms523016 KiB
15Runtime error219ms523000 KiB
subtask511/11
16Accepted3ms4328 KiB
17Accepted3ms4640 KiB
18Accepted3ms4656 KiB
subtask629/29
19Accepted54ms7644 KiB
20Accepted50ms7764 KiB
21Accepted28ms7756 KiB
22Accepted26ms7632 KiB
subtask70/30
23Runtime error234ms522576 KiB
24Runtime error233ms522576 KiB
25Runtime error237ms522344 KiB
26Runtime error190ms522116 KiB
27Runtime error188ms521884 KiB
28Runtime error236ms521884 KiB
29Runtime error237ms521876 KiB
30Runtime error186ms521876 KiB
31Runtime error231ms521884 KiB
32Runtime error182ms521872 KiB
33Runtime error181ms521684 KiB
34Runtime error226ms521684 KiB
35Runtime error180ms521688 KiB
36Runtime error172ms521684 KiB
37Runtime error219ms521692 KiB
38Runtime error223ms521688 KiB
39Runtime error172ms521684 KiB
40Runtime error230ms521680 KiB