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 fi first
#define se second
#define ll long long
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define pb push_back
#define max_rng *max_element
#define min_rng *min_element
#define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i)
#define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
template <class X, class Y>
inline bool maximize(X &x, Y y) {
return (x < y ? x = y, true : false);
}
template <class X, class Y>
inline bool minimize(X &x, Y y) {
return (x > y ? x = y, true : false);
}
template <class X>
inline void compress(vector<X> &a) {
sort(all(a));
a.resize(unique(all(a)) - a.begin());
}
int ctz(ll x) { return __builtin_ctzll(x); }
int lg(ll x) { return 63 - __builtin_clzll(x); }
int popcount(ll x) { return __builtin_popcount(x); }
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) {
return l + abs((ll) rng()) % (r - l + 1);
}
const ll oo = (ll) 1e17;
const int inf = (ll) 2e9 + 7, mod = (ll) 1e9 + 7;
const int mxn = 1e2 + 5, LOG = 50;
void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
void sub(int &x, int y) { x -= y; if (x < 0) x += mod; }
int n;
string s;
vector<vector<int>> op;
void backtrack(int p, vector<int> &cur) {
op.pb(cur);
if (p == 4) return;
forn(i, 0, 2) {
cur.pb(i);
backtrack(p + 1, cur);
cur.pop_back();
}
return;
}
int main(void) {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define TASK "cquery"
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
cin >> n >> s;
vector<int> cur;
backtrack(1, cur);
compress(op);
int cost[sz(op)];
int trans[sz(op)][3];
forn(i, 0, sz(op) - 1) {
vector<int> cur = op[i];
compress(cur);
cost[i] = sz(cur);
// for (auto v : op[i]) cout << v << ' ';
// cout << ": " << cost[i] << '\n';
forn(add, 0, 2) {
cur = op[i];
cur.pb(add);
if (sz(cur) > 3)
cur.erase(cur.begin());
trans[i][add] = lower_bound(all(op), cur) - op.begin();
}
}
// cout << trans[1][1] << '\n';
int m = sz(op);
ll f[2][m][m];
forn(a, 0, m - 1) forn(b, 0, m - 1) {
f[0][a][b] = f[1][a][b] = -oo;
}
f[0][0][0] = 0;
forn(i, 0, n - 1) {
int w = (s[i] == 'M' ? 2 : (s[i] == 'B'));
forn(a, 0, m - 1) forn(b, 0, m - 1) if (f[0][a][b] >= 0) {
maximize(f[1][ trans[a][w] ][b], f[0][a][b] + cost[ trans[a][w] ]);
maximize(f[1][a][ trans[b][w] ], f[0][a][b] + cost[ trans[b][w] ]);
}
forn(a, 0, m - 1) forn(b, 0, m - 1) {
f[0][a][b] = f[1][a][b];
f[1][a][b] = -oo;
}
}
ll ans = 0;
forn(a, 0, 40) forn(b, 0, 40) {
maximize(ans, f[0][a][b]);
}
cout << ans;
return 0;
}
Compilation message (stderr)
miners.cpp: In function 'int main()':
miners.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
miners.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |