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;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
const ll N = 1e5+5 , INF = 1e18 , MOD = 1e9+7;
ll dp[N][(1ll<<(4))][(1ll<<(4))];
ll n;
string s;
ll chartoll(char x){
if(x == 'M') return 0ll;
else if(x == 'B') return 1ll;
return 2ll;
}
ll go(ll i , ll mask1 , ll mask2 , vector<ll> mine1 , vector<ll> mine2){
if(i == n+1) return 0;
if(dp[i][mask1][mask2] != -1) return dp[i][mask1][mask2];
ll ans = 0;
ll curmask1 = mask1;
curmask1 |= (1ll<<(chartoll(s[i])));
ll c1 = __builtin_popcount(curmask1);
ll newmask1 = (1ll<<(chartoll(s[i])));
if(mine1.size() >= 1){
ll x = mine1[mine1.size()-1];
newmask1 |= (1ll<<x);
}
mine1.push_back(chartoll(s[i]));
ans = max(ans,c1+go(i+1,newmask1,mask2,mine1,mine2));
mine1.pop_back();
ll curmask2 = mask2;
curmask2 |= (1ll<<(chartoll(s[i])));
ll c2 = __builtin_popcount(curmask2);
ll newmask2 = (1ll<<(chartoll(s[i])));
if(mine2.size() >= 1){
ll x = mine2[mine2.size()-1];
newmask2 |= (1ll<<x);
}
mine2.push_back(chartoll(s[i]));
ans = max(ans,c2+go(i+1,mask1,newmask2,mine1,mine2));
mine2.pop_back();
return dp[i][mask1][mask2] = ans;
}
void solve(){
cin >> n >> s;
s = ' '+s;
if(n <= 20){
ll ans = -INF;
for(ll i = 0; i < (1ll<<n); i++){
vector<char> mine1,mine2;
for(ll j = 0; j < n; j++){
if(i&(1ll<<j)) mine1.push_back(s[j+1]);
else mine2.push_back(s[j+1]);
}
ll score = 0;
for(ll j = 0; j < mine1.size(); j++){
set<char> food;
for(ll k = j; k >= max(0ll,j-2); k--) food.insert(mine1[k]);
score += food.size();
}
for(ll j = 0; j < mine2.size(); j++){
set<char> food;
food.insert(mine2[j]);
for(ll k = j; k >= max(0ll,j-2); k--) food.insert(mine2[k]);
score += food.size();
}
ans = max(ans,score);
}
cout << ans;
return;
}
memset(dp,-1,sizeof dp);
vector<ll> a = {1,0,0,1,2,5};
cout << go(1,0,0,{},{});
}
int main(){
fast;
ll tc = 1;
// cin >> tc;
while(tc--) solve();
return 0;
}
Compilation message (stderr)
miners.cpp: In function 'void solve()':
miners.cpp:72:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(ll j = 0; j < mine1.size(); j++){
| ~~^~~~~~~~~~~~~~
miners.cpp:78:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(ll j = 0; j < mine2.size(); j++){
| ~~^~~~~~~~~~~~~~
# | 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... |