100472024-03-25 19:52:22111Nemzetközi Rántott Hús Fesztiválcpp17Time limit exceeded 56/100595ms178080 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
1Accepted3ms1828 KiB
2Accepted48ms3780 KiB
subtask27/7
3Accepted3ms2292 KiB
4Accepted3ms2468 KiB
subtask39/9
5Accepted8ms4136 KiB
6Accepted4ms4348 KiB
7Accepted32ms4756 KiB
subtask40/14
8Time limit exceeded545ms171992 KiB
9Time limit exceeded573ms172392 KiB
10Time limit exceeded564ms172384 KiB
11Time limit exceeded584ms172372 KiB
12Time limit exceeded563ms172272 KiB
13Time limit exceeded582ms172224 KiB
14Time limit exceeded595ms172528 KiB
15Time limit exceeded579ms172812 KiB
subtask511/11
16Accepted3ms4592 KiB
17Accepted3ms4392 KiB
18Accepted3ms4404 KiB
subtask629/29
19Accepted54ms6036 KiB
20Accepted50ms6028 KiB
21Accepted28ms6180 KiB
22Accepted25ms6380 KiB
subtask70/30
23Time limit exceeded573ms174868 KiB
24Time limit exceeded583ms174852 KiB
25Time limit exceeded566ms174648 KiB
26Time limit exceeded595ms175352 KiB
27Time limit exceeded587ms175760 KiB
28Time limit exceeded588ms176144 KiB
29Time limit exceeded578ms176264 KiB
30Time limit exceeded561ms175200 KiB
31Time limit exceeded595ms175744 KiB
32Time limit exceeded575ms177308 KiB
33Time limit exceeded579ms177756 KiB
34Time limit exceeded578ms178080 KiB
35Time limit exceeded579ms177976 KiB
36Time limit exceeded568ms173728 KiB
37Time limit exceeded582ms173856 KiB
38Time limit exceeded587ms173780 KiB
39Time limit exceeded588ms173756 KiB
40Time limit exceeded587ms177764 KiB