이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n;
int a[N];
int dp[N][4][4][4][4];
void maxi(int &a, int b)
{
if (a < b)
a = b;
}
int cost(int a, int b, int c)
{
int res = 0;
if (a < 3)
res++;
if (b < 3 && a != b)
res++;
if (c < 3 && a != c && b != c)
res++;
return res;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("file.inp","r",stdin);
cin >> n;
for (int i = 1; i <= n; i++)
{
char c;
cin >> c;
if (c == 'B')
a[i] = 0;
else if (c == 'M')
a[i] = 1;
else
a[i] = 2;
}
memset(dp, -1, sizeof(dp));
dp[1][3][3][3][3] = 0;
int ans = 0;
for (int i = 1; i <= n; i++)
for (int x = 0; x <= 3; x++)
for (int y = 0; y <= 3; y++)
for (int u = 0; u <= 3; u++)
for (int v = 0; v <= 3; v++)
{
int cur = dp[i][x][y][u][v];
if (cur < 0)
continue;
maxi(dp[i + 1][y][a[i]][u][v], cur + cost(x, y, a[i]));
maxi(dp[i + 1][x][y][v][a[i]], cur + cost(u, v, a[i]));
if (i == n)
{
maxi(ans, dp[i + 1][y][a[i]][u][v]);
maxi(ans, dp[i + 1][x][y][v][a[i]]);
}
}
cout << ans;
return 0;
}
# | 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... |