100482024-03-25 20:07:06111Nemzetközi Rántott Hús Fesztiválcpp17Time limit exceeded 56/100612ms181860 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>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);
	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 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;
				}
			}
			cout<<c[N]-c[i]-(l-1)<<' ';
		}
		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<<' ';
		}
	}
	cout<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1824 KiB
2Accepted4ms3908 KiB
subtask27/7
3Accepted3ms2404 KiB
4Accepted3ms2416 KiB
subtask39/9
5Accepted4ms4360 KiB
6Accepted4ms4576 KiB
7Accepted4ms4668 KiB
subtask40/14
8Time limit exceeded550ms175492 KiB
9Time limit exceeded574ms175784 KiB
10Time limit exceeded569ms175772 KiB
11Time limit exceeded578ms176032 KiB
12Time limit exceeded578ms176128 KiB
13Time limit exceeded566ms176368 KiB
14Time limit exceeded580ms176292 KiB
15Time limit exceeded545ms176272 KiB
subtask511/11
16Accepted3ms4160 KiB
17Accepted3ms4060 KiB
18Accepted3ms4200 KiB
subtask629/29
19Accepted4ms5760 KiB
20Accepted4ms5704 KiB
21Accepted4ms5964 KiB
22Accepted4ms6172 KiB
subtask70/30
23Time limit exceeded612ms178592 KiB
24Time limit exceeded577ms178692 KiB
25Time limit exceeded595ms178952 KiB
26Time limit exceeded542ms179632 KiB
27Time limit exceeded579ms179968 KiB
28Time limit exceeded583ms180228 KiB
29Time limit exceeded587ms180412 KiB
30Time limit exceeded591ms179212 KiB
31Time limit exceeded545ms179440 KiB
32Time limit exceeded586ms180700 KiB
33Time limit exceeded579ms181060 KiB
34Time limit exceeded578ms181336 KiB
35Time limit exceeded569ms181592 KiB
36Time limit exceeded578ms177416 KiB
37Time limit exceeded579ms177508 KiB
38Time limit exceeded564ms177764 KiB
39Time limit exceeded560ms177660 KiB
40Time limit exceeded586ms181860 KiB