100512024-03-25 20:17:00111Nemzetközi Rántott Hús Fesztiválcpp17Time limit exceeded 56/100569ms352368 KiB
#include <bits/stdc++.h>
using namespace std;

// #define int long long

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

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N=1000000;
	cin>>N;
	string S;
	for(int i=0;i<N;i++){
	  S+="MHK"[rand()%3];
	}
	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);
	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;
				}
			}
			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
1Accepted3ms2004 KiB
2Accepted4ms3704 KiB
subtask27/7
3Accepted3ms2228 KiB
4Accepted3ms2488 KiB
subtask39/9
5Accepted4ms4248 KiB
6Accepted4ms4284 KiB
7Accepted4ms4408 KiB
subtask40/14
8Time limit exceeded509ms341364 KiB
9Time limit exceeded521ms341588 KiB
10Accepted488ms341620 KiB
11Time limit exceeded523ms341592 KiB
12Accepted397ms341664 KiB
13Accepted361ms341852 KiB
14Accepted444ms341792 KiB
15Accepted495ms341872 KiB
subtask511/11
16Accepted3ms4112 KiB
17Accepted3ms4076 KiB
18Accepted3ms4088 KiB
subtask629/29
19Accepted4ms5568 KiB
20Accepted4ms5520 KiB
21Accepted4ms5788 KiB
22Accepted4ms5888 KiB
subtask70/30
23Time limit exceeded566ms346940 KiB
24Time limit exceeded532ms175364 KiB
25Time limit exceeded569ms175380 KiB
26Time limit exceeded556ms348432 KiB
27Time limit exceeded541ms349272 KiB
28Time limit exceeded540ms349776 KiB
29Time limit exceeded529ms350172 KiB
30Time limit exceeded541ms347784 KiB
31Time limit exceeded538ms348412 KiB
32Time limit exceeded508ms351124 KiB
33Accepted476ms351800 KiB
34Accepted419ms352280 KiB
35Accepted402ms352368 KiB
36Accepted398ms342724 KiB
37Accepted361ms342716 KiB
38Accepted442ms342704 KiB
39Accepted495ms342708 KiB
40Accepted493ms352144 KiB