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()
const int N=1e5+500;
int dp[N][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){
set<int> s; s.insert(x); s.insert(y); s.insert(z);
int cnt=s.size();
if(*s.begin()==0) cnt--;
return cnt;
};
L(i,0,n-1){
L(a1,0,3) L(a2,0,3) L(b1,0,3) L(b2,0,3){
if(dp[i][a1][a2][b1][b2]==-1) continue;
dp[i+1][a2][a[i]][b1][b2]=max(dp[i+1][a2][a[i]][b1][b2], dp[i][a1][a2][b1][b2]+profit(a1,a2,a[i]));
dp[i+1][a1][a2][b2][a[i]]=max(dp[i+1][a1][a2][b2][a[i]], dp[i][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][a1][a2][b1][b2]){
ans=dp[n][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... |