This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
Problem: IOI 2007 Miners
When: 2023-11-13 21:27:07
Author: JN
*/
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
using pii = pair<int, int>;
#define L(i,j,k) for(int i=(j);i<=(k);++i)
#define R(i,j,k) for(int i=(j);i>=(k);--i)
#define all(x) x.begin(), x.end()
int dp[2][4][4][4][4];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int n; cin>>n;
string s; cin>>s;
vector<int> a(n+1);
L(i,0,n-1){
if(s[i]=='M') a[i]=1;
if(s[i]=='B') a[i]=2;
if(s[i]=='F') a[i]=3;
}
memset(dp,-1,sizeof(dp));
dp[0][0][0][0][0]=0;
auto profit=[&](int x,int y,int z){
if(!x && !y) return 1;
if(!x) return (y!=z)+1;
if(x==y && y==z) return 1;
return (x!=y)+(y!=z)+(z!=x);
};
L(i,0,n-1){
int nw=i&1;int nx=nw^1;
L(a1,0,3) L(a2,0,3) L(b1,0,3) L(b2,0,3){
if(dp[nw][a1][a2][b1][b2]==-1) continue;
dp[nx][a2][a[i]][b1][b2]=max(dp[nx][a2][a[i]][b1][b2], dp[nw][a1][a2][b1][b2]+profit(a1,a2,a[i]));
dp[nx][a1][a2][b2][a[i]]=max(dp[nx][a1][a2][b2][a[i]], dp[nw][a1][a2][b1][b2]+profit(b1,b2,a[i]));
// cout<<a1<<' '<<a2<<' '<<b1<<' '<<b2<<' ';
// cout<<profit(a1,a2,a[i])<<' ';
// cout<<profit(b1,b2,a[i])<<'\n';
}
}
int ans=0;
L(a1,0,3) L(a2,0,3) L(b1,0,3) L(b2,0,3){
if(ans<dp[n&1][a1][a2][b1][b2]){
ans=dp[n&1][a1][a2][b1][b2];
//cout<<a1<<' '<<a2<<' '<<b1<<' '<<b2<<'\n';
}
}
cout<<ans<<'\n';
}
# | 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... |