# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
819822 | canadavid1 | Miners (IOI07_miners) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using i8 = int8_t;
vector<array<int,256>> dp;
int produce(i8 a,i8 b,i8 c)
{
bool s[4]{};
s[a] = true;
s[b] = true;
s[c] = true;
return ((int)s[1]) + ((int)s[2]) + ((int)s[3]);
}
int toidx(pair<i8,i8> h1,pair<i8,i8> h2)
{
//if(!h1.first || !h1.second || !h2.first || !h2.second) return 0;
return 4*4*4*h1.first + 4*4*h1.second + 4*h2.first + h2.second;
}
int toid(char s)
{
switch(s)
{
case 'B': return 1;
case 'M': return 2;
case 'F': return 3;
default: __builtin_unreachable();
}
}
int ct = 0;
int mct = 0;
int best(int off,string_view s,pair<i8,i8> h1={0,0}, pair<i8,i8> h2={0,0})
{
if(off >= s.size()) return 0;
ct++;
if (dp[off][toidx(h1,h2)]) return dp[off][toidx(h1,h2)];
mct++;
int max_produce = -1;
// h1
pair<i8,i8> h1p = {h1.second,toid(s[off])};
max_produce = max(max_produce,produce(h1.first,h1.second,toid(s[off]))+best(off+1,s,h1p,h2));
pair<i8,i8> h2p = {h2.second,toid(s[off])};
max_produce = max(max_produce,produce(h2.first,h2.second,toid(s[off]))+best(off+1,s,h1,h2p));
dp[off][toidx(h1,h2)] = max_produce;
return max_produce;
}
int main()
{
int N;
cin >> N;
string s(N,'\0');
dp.assign(N,array<int,256>{});
for(auto &&i : s) cin >> i;
cout << best(0,s) << "\n";
//cout << ct << " " << mct << "\n";
}