#include <bits/stdc++.h>
using namespace std;
///Dynamic Programming
///Determine the maximum amount of coal
int n;
int food[100005];
int memo[100005][4][4][4][4];
int different(int a, int b, int c) ///Returns the quantity of different types of food
{
if (a > b) swap(a, b);
if (b > c) swap(b, c);
if (a > b) swap(a, b);
int cnt = 1;
if (a != b) {
cnt++;
}
if (b != c) {
cnt++;
}
if (a == 0) cnt--; // a = 0 means no food
return cnt;
}
///State: actual index of food, two last from mine 1, two last from mine 2
int coal(int i, int X1, int X2, int Y1, int Y2)
{
if (i == n) return 0;
if (memo[i][X1][X2][Y1][Y2] == -1) {
int maximum = max(coal(i + 1, food[i], X1, Y1, Y2)
+ different(food[i], X1, X2), ///Send food to mine 1
coal(i + 1, X1, X2, food[i], Y1)
+ different(food[i], Y1, Y2)); ///Send food to mine 2
memo[i][X1][X2][Y1][Y2] = maximum;
}
return memo[i][X1][X2][Y1][Y2];
}
int main()
{
memset(memo, -1, sizeof(memo));
cin >> n;
for (int i = 0; i < n; i++) {
char c;
cin >> c;
if (c == 'M')
food[i] = 1;
else if (c == 'F')
food[i] = 2;
else if (c == 'B')
food[i] = 3;
}
int maximum = coal(0, 0, 0, 0, 0);
cout << maximum << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
100420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
100480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
100452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
100420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
100416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
100428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
100548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
100988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
101336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
102936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
150 ms |
107736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
110248 KB |
Output is correct |