답안 #236149

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
236149 2020-05-31T09:47:51 Z kartel Retro (COCI17_retro) C++14
62 / 100
500 ms 524292 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 n, m, i, j, b, B, len, ans, I, J;
char c[305][305];

bool great(string a, string b)
{
  	if (b == "x") return 1;
    if (sz(a) < sz(b)) return 0;
    if (sz(a) > sz(b)) return 1;
    return (a < 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;

    if (n * m * 1ll * n * n * n > ll(2e12))
    {
        pair <int, int> f[n + 5][m + 5][305];

        for (i = 1; i <= n; i++)
           for (j = 1; j <= m; j++)
             for (b = 0; b <= 300; b++)
                f[i][j][b].F = -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].F = 0;
        }


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

                if (j - 1 >= 1 && c[i - 1][j - 1] != '*')
                {
                    int B = b, len = f[i][j][b].F;

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

                    if (B >= 0 && (f[i - 1][j - 1][B].F == -1 || f[i - 1][j - 1][B].F < len || (f[i - 1][j - 1][B].F == len && B - b == 1)))
                    {
    //                    cerr << len << " " << i - 1 << " " << j - 1 << " " << B << el;
                        f[i - 1][j - 1][B].F = len;
                        f[i - 1][j - 1][B].S = j;
                    }

                }

                if (j >= 1 && c[i - 1][j] != '*')
                {
                    int B = b, len = f[i][j][b].F;

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

                    if (B >= 0 && (f[i - 1][j][B].F == -1 || f[i - 1][j][B].F < len || (f[i - 1][j][B].F == len && B - b == 1)))
                    {
    //                    cerr << len << " " << i - 1 << " " << j << " " << B << el;
                        f[i - 1][j][B].F = len;
                        f[i - 1][j][B].S = j;
                    }

                }

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

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

                    if (B >= 0 && (f[i - 1][j + 1][B].F == -1 || f[i - 1][j + 1][B].F < len || (f[i - 1][j + 1][B].F == len && B - b == 1)))
                    {
    //                    cerr << len << " " << i - 1 << " " << j + 1 << " " << B << el;
                        f[i - 1][j + 1][B].F = len;
                        f[i - 1][j + 1][B].S = j;
                    }
                }
            }
        }

        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= m; j++)
            {
                if (i == 1 || c[i - 1][j] == '*' || (j - 1 >= 1 && c[i - 1][j - 1] == '*') || (j + 1 <= m && c[i - 1][j + 1] == '*'))
                {
                    if (ans < f[i][j][0].F)
                        ans = f[i][j][0].F, I = i, J = j;
                }
            }
        }

        string s = "";

        cout << ans << el;

        b = 0;

        while (I != n)
        {
            int nb = b;

            if (c[I][J] != '.')
            {
                if (c[I][J] == ')') nb++;
                               else nb--;
                s += c[I][J];
            }

            J = f[I][J][b].S;
            I++;

            b = nb;
        }

        reverse(s.begin(), s.end());

        cout << s;
    }
    else
    {
        string F[n + 5][m + 5][305];

        for (i = 1; i <= n; i++)
           for (j = 1; j <= m; j++)
             for (b = 0; b <= 300; b++)
                F[i][j][b] = "x";

        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] = "";
        }


        for (i = n; i >= 2; i--)
        {
            for (j = 1; j <= m; j++)
                for (b = 0; b <= 300; b++)
            {
                if (F[i][j][b] == "x") continue;
  //              cerr << i << " " << j << " " << b << " " << F[i][j][b] << el;

                if (j - 1 >= 1 && c[i - 1][j - 1] != '*')
                {
                    int B;B = b;
                    string x = F[i][j][b];

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

                    if (B >= 0 && great(x, F[i - 1][j - 1][B]))
                    {
                        //cout << i << " " << j << " " << B << " " << b << " " << F[i][j][b] << " " << F[i - 1][j - 1][B] << el;
//    //                    cerr << len << " " << i - 1 << " " << j - 1 << " " << B << el;
                        F[i - 1][j - 1][B] = x;
                    }

                }

                if (j >= 1 && c[i - 1][j] != '*')
                {
                    int B = b;

                    string x = F[i][j][b];

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

                    if (B >= 0 && great(x, F[i - 1][j][B]))
                    {
//                        cerr << F[i][j][b] << " " << F[i - 1][j][B] << el;
    //                    cerr << len << " " << i - 1 << " " << j << " " << B << el;
                        F[i - 1][j][B] = x;
                    }

                }

                if (j + 1 <= m && c[i - 1][j + 1] != '*')
                {
                    int B = b;

                    string x = F[i][j][b];

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

                    if (B >= 0 && great(x, F[i - 1][j + 1][B]))
                    {
//                        cerr << F[i][j][b] << " " << F[i - 1][j + 1][B] << el;
                        F[i - 1][j + 1][B] = x;
                    }
                }
            }
        }
        string s = "";
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= m; j++)
            {
                if (i == 1 || c[i - 1][j] == '*' || (j - 1 >= 1 && c[i - 1][j - 1] == '*') || (j + 1 <= m && c[i - 1][j + 1] == '*'))
                {
                    if (great(F[i][j][0], s))
                        s = F[i][j][0];
                }
            }
        }
//        s.erase(0, 1);
        cout << sz(s) << el << s;
    }
 }

//
//110000
//1100
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1920 KB Output is correct
2 Correct 8 ms 4608 KB Output is correct
3 Correct 7 ms 3072 KB Output is correct
4 Correct 13 ms 6016 KB Output is correct
5 Correct 25 ms 20352 KB Output is correct
6 Correct 87 ms 53112 KB Output is correct
7 Correct 120 ms 78692 KB Output is correct
8 Correct 66 ms 39672 KB Output is correct
9 Correct 146 ms 82808 KB Output is correct
10 Correct 187 ms 110968 KB Output is correct
11 Runtime error 244 ms 524292 KB Execution killed with signal 9 (could be triggered by violating memory limits)
12 Runtime error 238 ms 524288 KB Execution killed with signal 9 (could be triggered by violating memory limits)
13 Execution timed out 1127 ms 493024 KB Time limit exceeded
14 Execution timed out 1127 ms 503340 KB Time limit exceeded
15 Partially correct 211 ms 221048 KB Partially correct
16 Partially correct 202 ms 221036 KB Partially correct
17 Partially correct 178 ms 186148 KB Partially correct
18 Partially correct 171 ms 186104 KB Partially correct
19 Partially correct 215 ms 222460 KB Partially correct
20 Partially correct 204 ms 222456 KB Partially correct