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
*/
#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
vector<int> decode(int val)
{
vector<int> ans;
ans.push_back(val % 4);
val /= 4;
ans.push_back(val % 4);
reverse(ans.begin(), ans.end());
for(int i = 0; i < ans.size(); i++)
{
if(ans[i] == 0)
{
swap(ans[i], ans.back());
ans.pop_back();
i--;
}
}
return ans;
}
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 4 * food[food.size() - 2] + food[food.size() - 1];
throw "too much food";
}
int coal(vector<int> &food)
{
int count[4] = {};
for(int f : food)
count[f]++;
int ans = 0;
for(int i = 1; i < 4; i++)
{
if(count[i] > 0)
ans++;
}
return ans;
}
pair<int, int> addFood(int val, int newVal)
{
vector<int> oldFood = decode(val);
oldFood.push_back(newVal);
int res = coal(oldFood);
return make_pair(encode(oldFood), res);
}
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 = 16;
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 'std::vector<int> decode(int)':
miners.cpp:26:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i = 0; i < ans.size(); i++)
| ~~^~~~~~~~~~~~
miners.cpp: In function 'int main()':
miners.cpp:124:42: warning: 'num' may be used uninitialized in this function [-Wmaybe-uninitialized]
124 | 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... |