10051 | 2024-03-25 20:17:00 | 111 | Nemzetközi Rántott Hús Fesztivál | cpp17 | Time limit exceeded 56/100 | 569ms | 352368 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;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 2004 KiB | ||||
2 | Accepted | 4ms | 3704 KiB | ||||
subtask2 | 7/7 | ||||||
3 | Accepted | 3ms | 2228 KiB | ||||
4 | Accepted | 3ms | 2488 KiB | ||||
subtask3 | 9/9 | ||||||
5 | Accepted | 4ms | 4248 KiB | ||||
6 | Accepted | 4ms | 4284 KiB | ||||
7 | Accepted | 4ms | 4408 KiB | ||||
subtask4 | 0/14 | ||||||
8 | Time limit exceeded | 509ms | 341364 KiB | ||||
9 | Time limit exceeded | 521ms | 341588 KiB | ||||
10 | Accepted | 488ms | 341620 KiB | ||||
11 | Time limit exceeded | 523ms | 341592 KiB | ||||
12 | Accepted | 397ms | 341664 KiB | ||||
13 | Accepted | 361ms | 341852 KiB | ||||
14 | Accepted | 444ms | 341792 KiB | ||||
15 | Accepted | 495ms | 341872 KiB | ||||
subtask5 | 11/11 | ||||||
16 | Accepted | 3ms | 4112 KiB | ||||
17 | Accepted | 3ms | 4076 KiB | ||||
18 | Accepted | 3ms | 4088 KiB | ||||
subtask6 | 29/29 | ||||||
19 | Accepted | 4ms | 5568 KiB | ||||
20 | Accepted | 4ms | 5520 KiB | ||||
21 | Accepted | 4ms | 5788 KiB | ||||
22 | Accepted | 4ms | 5888 KiB | ||||
subtask7 | 0/30 | ||||||
23 | Time limit exceeded | 566ms | 346940 KiB | ||||
24 | Time limit exceeded | 532ms | 175364 KiB | ||||
25 | Time limit exceeded | 569ms | 175380 KiB | ||||
26 | Time limit exceeded | 556ms | 348432 KiB | ||||
27 | Time limit exceeded | 541ms | 349272 KiB | ||||
28 | Time limit exceeded | 540ms | 349776 KiB | ||||
29 | Time limit exceeded | 529ms | 350172 KiB | ||||
30 | Time limit exceeded | 541ms | 347784 KiB | ||||
31 | Time limit exceeded | 538ms | 348412 KiB | ||||
32 | Time limit exceeded | 508ms | 351124 KiB | ||||
33 | Accepted | 476ms | 351800 KiB | ||||
34 | Accepted | 419ms | 352280 KiB | ||||
35 | Accepted | 402ms | 352368 KiB | ||||
36 | Accepted | 398ms | 342724 KiB | ||||
37 | Accepted | 361ms | 342716 KiB | ||||
38 | Accepted | 442ms | 342704 KiB | ||||
39 | Accepted | 495ms | 342708 KiB | ||||
40 | Accepted | 493ms | 352144 KiB |