Submission #238207

# Submission time Handle Problem Language Result Execution time Memory
238207 2020-06-10T08:36:59 Z kartel Retro (COCI17_retro) C++14
40 / 100
200 ms 133880 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#define F first
#define S second
#define pb push_back
#define N +100500
#define M ll(1e9 + 7)
#define sz(x) (int)x.size()
#define re return
#define oo ll(1e9)
#define el '\n'
#define pii pair <int, int>
using namespace std;
//using namespace __gnu_pbds;
//typedef tree <int, null_type, less_equal <int> , rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef long double ld;

int f[302][302][302];
int n, m, i, j, b, B, len, ans, I, J, sx, sy;
char c[305][305];
queue <pair <int, pair <int, int> > > q;
vector < pair <int, pair <int, int> > > v[2], start;
bool mrk[305][305][305], mk[305][305][305];

void put(int i, int j, int b, int ni, int nj)
{
    if (c[ni][nj] == '*') return;

    int B = b;

    if (c[ni][nj] == '(') B++;else
    if (c[ni][nj] == ')') B--;

    if (c[ni][nj] == '.' && mrk[ni][nj][B] && f[ni][nj][B] == f[i][j][b] && !mk[ni][nj][B])
    {
        mk[ni][nj][B] = 1;
        q.push({ni, {nj, B}});
    }
    else if (c[ni][nj] == '(' && mrk[ni][nj][B] && f[ni][nj][B] == f[i][j][b] + 1 && !mk[ni][nj][B])
    {
        mk[ni][nj][B] = 1;
        q.push({ni, {nj, B}});
    }
}

int main()
{
    srand(time(0));
    ios_base::sync_with_stdio(0);
    iostream::sync_with_stdio(0);
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);

//    in("input.txt");
//    out("output.txt");

    cin >> n >> m;

    for (i = 1; i <= n; i++)
       for (j = 1; j <= m; j++)
         for (b = 0; b <= 300; b++)
            f[i][j][b] = -1;

    for (i = 1; i <= n; i++)
      for (j = 1; j <= m; j++)
    {
        cin >> c[i][j];

        if (c[i][j] == 'M') f[i][j][0] = 0, sx = i, sy = j;
    }


    for (i = n; i >= 2; i--)
    {
        for (j = 1; j <= m; j++)
            for (b = 0; b <= 300; b++)
        {
            if (f[i][j][b] == -1) continue;


            for (int st = -1; st < 2; st++)
            {
                if (j + st >= 1 && j + st <= m && c[i - 1][j + st] != '*')
                {
                    int B = b, len = f[i][j][b];

                    if (c[i - 1][j + st] == '(') B++, len++; else
                    if (c[i - 1][j + st] == ')') B--, len++;

                    if (B >= 0 && (f[i - 1][j + st][B] == -1 || f[i - 1][j + st][B] < len))
                        f[i - 1][j + st][B] = len;

                }
            }
        }
    }

    for (i = 1; i <= n; i++)
        for (j = 1; j <= m; j++)
             ans = max(ans, f[i][j][0]);

    cout << ans << el;

    mrk[sx][sy][0] = 1;

    for (i = n; i >= 2; i--)
    {
        for (j = 1; j <= m; j++)
            for (b = 0; b <= 300; b++)
                {
                    if (!mrk[i][j][b]) continue;

                    for (int st = -1; st < 2; st++)
                    {
                        int ni = i - 1, nj = j + st;
//                        cerr << ni << " " << nj << " " << i << " " << j << el;

                        if (j < 1 || j > m) continue;

                        if (c[ni][nj] == '(' && f[ni][nj][b + 1] == f[i][j][b] + 1) mrk[ni][nj][b + 1] = 1;
                        else if (b > 0 && c[ni][nj] == ')' && f[ni][nj][b - 1] == f[i][j][b] + 1) mrk[ni][nj][b - 1] = 1;
                        else if (f[ni][nj][b] == f[i][j][b] && c[ni][nj] == '.') mrk[ni][nj][b] = 1;
                    }
                }
    }

    start.pb({sx, {sy, 0}});

    for (int len = 0; len < ans; len++)
    {
        v[0].clear();
        v[1].clear();

        for (auto pos : start)
        {
            i = pos.F;
            j = pos.S.F;
            b = pos.S.S;

            for (int st = -1; st < 2; st++)
            {
                int ni = i - 1;
                int nj = j + st;
                if (nj < 1 || nj > m) continue;

                put(i, j, b, ni, nj);
            }
        }

        while (sz(q))
        {
            i = q.front().F;
            j = q.front().S.F;
            b = q.front().S.S;q.pop();
            if (c[i][j] == '(')
            {
                v[0].pb({i, {j, b}});
                continue;
            }
            if (c[i][j] == ')')
            {
                v[1].pb({i, {j, b}});
                continue;
            }

            for (int st = -1; st < 2; st++)
            {
                int ni = i - 1;
                int nj = j + st;
                if (nj < 1 || nj > m) continue;

                put(i, j, b, ni, nj);
            }
        }
        if (sz(v[0]) > 0)
        {
            start = v[0];
            cout << "(";
        }
        else
        {
            start = v[1];
            cout << ")";
        }
    }
}

//
//110000
//1100
# Verdict Execution time Memory Grader output
1 Partially correct 5 ms 512 KB Partially correct
2 Partially correct 5 ms 896 KB Partially correct
3 Partially correct 5 ms 768 KB Partially correct
4 Partially correct 6 ms 1024 KB Partially correct
5 Partially correct 6 ms 2432 KB Partially correct
6 Partially correct 13 ms 7552 KB Partially correct
7 Partially correct 15 ms 11008 KB Partially correct
8 Partially correct 12 ms 7168 KB Partially correct
9 Partially correct 17 ms 12672 KB Partially correct
10 Partially correct 22 ms 16896 KB Partially correct
11 Partially correct 166 ms 127964 KB Partially correct
12 Partially correct 158 ms 122232 KB Partially correct
13 Partially correct 80 ms 57952 KB Partially correct
14 Partially correct 76 ms 56296 KB Partially correct
15 Partially correct 183 ms 129272 KB Partially correct
16 Partially correct 173 ms 131320 KB Partially correct
17 Partially correct 158 ms 115192 KB Partially correct
18 Partially correct 143 ms 112888 KB Partially correct
19 Partially correct 200 ms 133880 KB Partially correct
20 Partially correct 170 ms 129656 KB Partially correct