이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O1")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 100001,INF=1e12;
int n;
string s;
//{i,{ {m1,{b1,f1}} ,{m2,{b2,f2}} }};
map<pair<int , pair<pair<int,pair<int,int>>,pair<int,pair<int,int>>>>,int>dp;
int solve(int i,int M1,int B1,int F1,int M2,int B2,int F2)
{
//cout<<i<<" "<<M1<<" "<<B1<<" "<<F1<<" "<<M2<<" "<<B2<<" "<<F2<<endl;
if(i==n) return 0;
int ans=0;
if(dp.count({i , {{M1,{B1,F1}}, {M2,{B2,F2}} }})) return dp[{i , {{M1,{B1,F1}}, {M2,{B2,F2}} }}];
if(s[i]=='M'){
int m1=M1,b1=B1,f1=F1;
m1=0,b1++,f1++;
if(B1==-1 || b1>2) b1=-1;
if(F1==-1 || f1>2) f1=-1;
ans = solve(i+1,m1,b1,f1,M2,B2,F2)+((m1>=0)+(b1>=0)+(f1>=0));
m1=M2,b1=B2,f1=F2;
m1=0,b1++,f1++;
if(B2==-1 || b1>2) b1=-1;
if(F2==-1 || f1>2) f1=-1;
ans=max(ans,solve(i+1,M1,B1,F1,m1,b1,f1)+((m1>=0)+(b1>=0)+(f1>=0)));
}
else if(s[i]=='B'){
int m1=M1,b1=B1,f1=F1;
m1++,b1=0,f1++;
if(M1==-1 || m1>2) m1=-1;
if(F1==-1 || f1>2) f1=-1;
ans = solve(i+1,m1,b1,f1,M2,B2,F2)+((m1>=0)+(b1>=0)+(f1>=0));
m1=M2,b1=B2,f1=F2;
m1++,b1=0,f1++;
if(M2==-1 || m1>2) m1=-1;
if(F2==-1 || f1>2) f1=-1;
ans=max(ans,solve(i+1,M1,B1,F1,m1,b1,f1)+((m1>=0)+(b1>=0)+(f1>=0)));
}
else if(s[i]=='F'){
int m1=M1,b1=B1,f1=F1;
m1++,b1++,f1=0;
if(M1==-1 || m1>2) m1=-1;
if(B1==-1 || b1>2) b1=-1;
ans = solve(i+1,m1,b1,f1,M2,B2,F2)+((m1>=0)+(b1>=0)+(f1>=0));
m1=M2,b1=B2,f1=F2;
m1++,b1++,f1=0;
if(M2==-1 || m1>2) m1=-1;
if(B2==-1 || b1>2) b1=-1;
ans=max(ans,solve(i+1,M1,B1,F1,m1,b1,f1)+((m1>=0)+(b1>=0)+(f1>=0)));
}
return dp[{i , {{M1,{B1,F1}}, {M2,{B2,F2}} }}]=ans;
}
int32_t main()
{
//freopen("abc.in", "r", stdin);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n ;
cin>>s;
cout<<solve(0,-1,-1,-1,-1,-1,-1);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |