100592024-03-25 20:41:39111Nemzetközi Rántott Hús Fesztiválcpp17Time limit exceeded 70/100564ms365152 KiB
#include <bits/stdc++.h>
using namespace std;

// #define int long long

char bf[11000000];
char*bfe=bf;

void wc(char c) {
	*bfe++ = c;
}

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

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

int qk(int l,int r){
	int i=__lg(r-l);
	return min(tk[i][l],tk[i][r-(1<<i)]);
};

int qh(int l,int r){
	int i=__lg(r-l);
	return min(th[i][l],th[i][r-(1<<i)]);
};

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
	string S;
	cin>>N>>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++){
	  int d=(1<<j-1);
	  int e=N+1-d;
		for(int l=0;l<e;l++){
			tk[j][l]=min(tk[j-1][l],tk[j-1][l+d]);
			th[j][l]=min(th[j-1][l],th[j-1][l+d]);
		}
	}
	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),bfe-bf,stdout);
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2004 KiB
2Accepted4ms3704 KiB
subtask27/7
3Accepted3ms2364 KiB
4Accepted3ms2536 KiB
subtask39/9
5Accepted4ms4036 KiB
6Accepted4ms4248 KiB
7Accepted4ms4464 KiB
subtask414/14
8Accepted425ms345672 KiB
9Accepted400ms346544 KiB
10Accepted375ms346696 KiB
11Accepted372ms346840 KiB
12Accepted300ms350628 KiB
13Accepted308ms352140 KiB
14Accepted345ms348244 KiB
15Accepted386ms347584 KiB
subtask511/11
16Accepted3ms4196 KiB
17Accepted3ms4156 KiB
18Accepted3ms4224 KiB
subtask629/29
19Accepted4ms5456 KiB
20Accepted4ms5724 KiB
21Accepted4ms5948 KiB
22Accepted4ms6168 KiB
subtask70/30
23Accepted467ms354888 KiB
24Time limit exceeded555ms178876 KiB
25Accepted469ms354956 KiB
26Accepted479ms357784 KiB
27Time limit exceeded563ms181624 KiB
28Time limit exceeded546ms181640 KiB
29Accepted460ms361328 KiB
30Accepted462ms357940 KiB
31Time limit exceeded564ms181596 KiB
32Accepted449ms363196 KiB
33Accepted499ms364172 KiB
34Accepted414ms364932 KiB
35Accepted351ms365152 KiB
36Accepted333ms351812 KiB
37Accepted279ms353420 KiB
38Accepted340ms349388 KiB
39Accepted388ms348956 KiB
40Accepted433ms364840 KiB