10047 | 2024-03-25 19:52:22 | 111 | Nemzetközi Rántott Hús Fesztivál | cpp17 | Time limit exceeded 56/100 | 595ms | 178080 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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1828 KiB | ||||
2 | Accepted | 48ms | 3780 KiB | ||||
subtask2 | 7/7 | ||||||
3 | Accepted | 3ms | 2292 KiB | ||||
4 | Accepted | 3ms | 2468 KiB | ||||
subtask3 | 9/9 | ||||||
5 | Accepted | 8ms | 4136 KiB | ||||
6 | Accepted | 4ms | 4348 KiB | ||||
7 | Accepted | 32ms | 4756 KiB | ||||
subtask4 | 0/14 | ||||||
8 | Time limit exceeded | 545ms | 171992 KiB | ||||
9 | Time limit exceeded | 573ms | 172392 KiB | ||||
10 | Time limit exceeded | 564ms | 172384 KiB | ||||
11 | Time limit exceeded | 584ms | 172372 KiB | ||||
12 | Time limit exceeded | 563ms | 172272 KiB | ||||
13 | Time limit exceeded | 582ms | 172224 KiB | ||||
14 | Time limit exceeded | 595ms | 172528 KiB | ||||
15 | Time limit exceeded | 579ms | 172812 KiB | ||||
subtask5 | 11/11 | ||||||
16 | Accepted | 3ms | 4592 KiB | ||||
17 | Accepted | 3ms | 4392 KiB | ||||
18 | Accepted | 3ms | 4404 KiB | ||||
subtask6 | 29/29 | ||||||
19 | Accepted | 54ms | 6036 KiB | ||||
20 | Accepted | 50ms | 6028 KiB | ||||
21 | Accepted | 28ms | 6180 KiB | ||||
22 | Accepted | 25ms | 6380 KiB | ||||
subtask7 | 0/30 | ||||||
23 | Time limit exceeded | 573ms | 174868 KiB | ||||
24 | Time limit exceeded | 583ms | 174852 KiB | ||||
25 | Time limit exceeded | 566ms | 174648 KiB | ||||
26 | Time limit exceeded | 595ms | 175352 KiB | ||||
27 | Time limit exceeded | 587ms | 175760 KiB | ||||
28 | Time limit exceeded | 588ms | 176144 KiB | ||||
29 | Time limit exceeded | 578ms | 176264 KiB | ||||
30 | Time limit exceeded | 561ms | 175200 KiB | ||||
31 | Time limit exceeded | 595ms | 175744 KiB | ||||
32 | Time limit exceeded | 575ms | 177308 KiB | ||||
33 | Time limit exceeded | 579ms | 177756 KiB | ||||
34 | Time limit exceeded | 578ms | 178080 KiB | ||||
35 | Time limit exceeded | 579ms | 177976 KiB | ||||
36 | Time limit exceeded | 568ms | 173728 KiB | ||||
37 | Time limit exceeded | 582ms | 173856 KiB | ||||
38 | Time limit exceeded | 587ms | 173780 KiB | ||||
39 | Time limit exceeded | 588ms | 173756 KiB | ||||
40 | Time limit exceeded | 587ms | 177764 KiB |