This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
16
MMBMBBBBMMMMMBMB
*/
/*input
6
MBMFFB
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <unistd.h>
using namespace std;
using namespace __gnu_pbds;
#define ws(x) cerr << #x << " is " << x << endl
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> orderedTree;
typedef long long ll;
#define mp make_pair
int encode(vector<int> &food)
{
if(food.size() == 0)
return 0;
else if (food.size() == 1)
return food.back();
else if (food.size() >= 2)
return 3 * food[food.size() - 2] + food[food.size() - 1];
}
// int encode(vector<int> &food)
// {
// cout << "encodinu:" << endl;
// for(int i : food)
// cout << i << " ";
// cout << endl;
// cout << encode1(food) << endl;
// return encode1(food);
// }
pair<int, int> addFood(int val, int newVal)
{
// cout << "val = " << val << endl;
// cout << "newVal = " << newVal << endl;
vector<int> oldFood;
if((val < 4) && (val > 0))
oldFood.push_back(val);
else if (val >= 4)
{
oldFood.push_back((val - 1) / 3);
if(val %3 == 0)
oldFood.push_back(3);
else
oldFood.push_back(val % 3);
}
// cout << "food = " << endl;
// for(int f : oldFood)
// cout << f << " ";
// cout << endl;
// reverse(oldFood.begin(), oldFood.end());
oldFood.push_back(newVal);
int count[4] = {};
for(int f : oldFood)
{
count[f]++;
}
int coal = 0;
for(int i = 1; i < 4; i++)
{
if(count[i] > 0)
coal++;
}
// assert(encode(oldFood) < 13);
return make_pair(encode(oldFood), coal);
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int N;
cin >> N;
string food;
cin >> food;
const int numOps = 13;
int states[2][numOps][numOps];
const int MININF = - 5 * N - (1e6);
for(int x = 0; x < numOps; x++)
{
for(int y = 0; y < numOps; y++)
{
states[0][x][y] = MININF;
states[0][x][y] = MININF;
}
}
states[0][0][0] = 0;
for(int i = 0; i < N; i++)
{
int num;
if(food[i] == 'M')
num = 1;
else if (food[i] == 'B')
num = 2;
else if (food[i] == 'F')
num = 3;
// cout << "num = " << num << endl;
int curId = i % 2, nextId = (i + 1) % 2;
for(int x = 0; x < numOps; x++)
{
for(int y = 0; y < numOps; y++)
states[nextId][x][y] = MININF;
}
for(int x = 0; x < numOps; x++)
{
for(int y = 0; y < numOps; y++)
{
pair<int, int> next1 = addFood(x, num);
states[nextId][next1.first][y] = max(states[nextId][next1.first][y], states[curId][x][y] + next1.second);
pair<int, int> next2 = addFood(y, num);
states[nextId][x][next2.first] = max(states[nextId][x][next2.first], states[curId][x][y] + next2.second);
}
}
}
int ans = 0;
for(int x = 0; x < numOps; x++)
{
for(int y = 0; y < numOps; y++)
ans = max(ans, states[N%2][x][y]);
}
cout << ans;
return 0;
}
Compilation message (stderr)
miners.cpp: In function 'int encode(std::vector<int>&)':
miners.cpp:31:1: warning: control reaches end of non-void function [-Wreturn-type]
31 | }
| ^
miners.cpp: In function 'int main()':
miners.cpp:132:42: warning: 'num' may be used uninitialized in this function [-Wmaybe-uninitialized]
132 | pair<int, int> next2 = addFood(y, num);
| ^
# | 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... |