This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast","unroll-loops")
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, ans = 0, dp[2][4][4][4][4] = {}, reset[4][4][4][4], precalc[4][4][4];
cin >> n;
string s;
cin >> s;
auto cost = [&](int x, int y, int z) {
int cnt[4] = {}, ans = 0;
++cnt[x], ++cnt[y], ++cnt[z];
for (int i = 1; i < 4; ++i)
ans += !!cnt[i];
return ans;
};
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
for (int k = 0; k < 4; ++k) {
precalc[i][j][k] = cost(i, j, k);
for (int l = 0; l < 4; ++l)
reset[i][j][k][l] = -10000;
}
memcpy(dp[1], reset, sizeof(reset));
dp[1][0][0][0][0] = 0;
for (int i = 0; i < n; ++i) {
int cur = s[i] == 'M' ? 1 : s[i] == 'F' ? 2 : 3;
memcpy(dp[i & 1], reset, sizeof(reset));
for (int fll = 0; fll < 4; ++fll)
for (int fl = !!fll; fl < 4; ++fl)
for (int sll = 0; sll < 4; ++sll)
for (int sl = !!sll; sl < 4; ++sl)
if (dp[(i & 1) ^ 1][fll][fl][sll][sl] >= 0) {
dp[i & 1][fl][cur][sll][sl] = max(dp[i & 1][fl][cur][sll][sl], dp[(i & 1) ^ 1][fll][fl][sll][sl] + precalc[fll][fl][cur]);
dp[i & 1][fll][fl][sl][cur] = max(dp[i & 1][fll][fl][sl][cur], dp[(i & 1) ^ 1][fll][fl][sll][sl] + precalc[sll][sl][cur]);
}
}
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
for (int k = 0; k < 4; ++k)
for (int l = 0; l < 4; ++l)
ans = max(ans, dp[(n - 1) & 1][i][j][k][l]);
cout << ans;
}
# | 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... |