# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
236171 | NONAME | Retro (COCI17_retro) | C++17 | 270 ms | 320632 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#define N 100500
#define oo ll(1e16)
#define ft first
#define sd second
#define pb push_back
#define ppb pop_back
#define el '\n'
#define elf endl
#define base ll(1e9 + 7)
using namespace std;
typedef long long ll;
typedef long double ld;
int n, m, f[300][300][300];
pair <int, int> pr[300][300][300];
string s[N];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> s[i];
for (int i = 0; i < n - i - 1; i++)
swap(s[i], s[n - i - 1]);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
for (int k = 0; k < n; k++)
f[i][j][k] = -1, pr[i][j][k] = {-1, -1};
for (int i = 0; i < m; i++)
if (s[0][i] == 'M')
f[0][i][0] = 0;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < m; j++) {
if (j - 1 >= 0 && s[i + 1][j - 1] != '*') {
if (s[i + 1][j - 1] == '.') {
for (int k = 0; k < n; k++)
if (f[i][j][k] != -1) {
if (f[i][j][k] > f[i + 1][j - 1][k])
pr[i + 1][j - 1][k] = {j, k};
f[i + 1][j - 1][k] = max(f[i + 1][j - 1][k], f[i][j][k]);
}
} else {
if (s[i + 1][j - 1] == '(') {
for (int k = 0; k < n; k++)
if (f[i][j][k] != -1) {
if (f[i][j][k] + 1 >= f[i + 1][j - 1][k + 1])
pr[i + 1][j - 1][k + 1] = {j, k};
f[i + 1][j - 1][k + 1] = max(f[i + 1][j - 1][k + 1], f[i][j][k] + 1);
}
} else {
for (int k = 1; k < n; k++)
if (f[i][j][k] != -1) {
if (f[i][j][k] + 1 > f[i + 1][j - 1][k - 1])
pr[i + 1][j - 1][k - 1] = {j, k};
f[i + 1][j - 1][k - 1] = max(f[i + 1][j - 1][k - 1], f[i][j][k] + 1);
}
}
}
}
if (s[i + 1][j] != '*') {
if (s[i + 1][j] == '.') {
for (int k = 0; k < n; k++)
if (f[i][j][k] != -1) {
if (f[i][j][k] > f[i + 1][j][k])
pr[i + 1][j][k] = {j, k};
f[i + 1][j][k] = max(f[i + 1][j][k], f[i][j][k]);
}
} else {
if (s[i + 1][j] == '(') {
for (int k = 0; k < n; k++)
if (f[i][j][k] != -1) {
if (f[i][j][k] + 1 >= f[i + 1][j][k + 1])
pr[i + 1][j][k + 1] = {j, k};
f[i + 1][j][k + 1] = max(f[i + 1][j][k + 1], f[i][j][k] + 1);
}
} else {
for (int k = 1; k < n; k++)
if (f[i][j][k] != -1) {
if (f[i][j][k] + 1 > f[i + 1][j][k - 1])
pr[i + 1][j][k - 1] = {j, k};
f[i + 1][j][k - 1] = max(f[i + 1][j][k - 1], f[i][j][k] + 1);
}
}
}
}
if (j + 1 < m && s[i + 1][j + 1] != '*') {
if (s[i + 1][j + 1] == '.') {
for (int k = 0; k < n; k++)
if (f[i][j][k] != -1) {
if (f[i][j][k] > f[i + 1][j + 1][k])
pr[i + 1][j + 1][k] = {j, k};
f[i + 1][j + 1][k] = max(f[i + 1][j + 1][k], f[i][j][k]);
}
} else {
if (s[i + 1][j + 1] == '(') {
for (int k = 0; k < n; k++)
if (f[i][j][k] != -1) {
if (f[i][j][k] + 1 >= f[i + 1][j + 1][k + 1])
pr[i + 1][j + 1][k + 1] = {j, k};
f[i + 1][j + 1][k + 1] = max(f[i + 1][j + 1][k + 1], f[i][j][k] + 1);
}
} else {
for (int k = 1; k < n; k++)
if (f[i][j][k] != -1) {
if (f[i][j][k] + 1 > f[i + 1][j + 1][k - 1])
pr[i + 1][j + 1][k - 1] = {j, k};
f[i + 1][j + 1][k - 1] = max(f[i + 1][j + 1][k - 1], f[i][j][k] + 1);
}
}
}
}
}
int mx = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
mx = max(mx, f[i][j][0]);
cout << mx << el;
vector <string> v;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (f[i][j][0] != mx)
continue;
int x = i, y = j, k = 0;
string res = "";
while (x != 0) {
int old_y = y;
if (s[x][y] == '.') {
y = pr[x][y][k].ft;
k = pr[x][old_y][k].sd;
x--;
continue;
}
res += s[x][y];
y = pr[x][y][k].ft;
k = pr[x][old_y][k].sd;
x--;
}
reverse(res.begin(), res.end());
v.pb(res);
}
sort(v.begin(), v.end());
cout << v[0];
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |