답안 #648407

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
648407 2022-10-06T10:58:00 Z bebra Retro (COCI17_retro) C++17
0 / 100
261 ms 524288 KB
#include <bits/stdc++.h>
using namespace std;

#define dbg(x) cerr << #x << ": " << x << endl;

const int INF = 1e9;
const int MAX_N = 300 + 1;
const int MAX_BALANCE = 300 / 2 + 5;
char grid[MAX_N][MAX_N];

struct State {
    int len;
    // 0 - open, 1 - close
    bitset<MAX_N> sequence;

    State(int _len = -INF) : len(_len) {}

    bool operator<(const State& other) const {
        if (len == other.len) {
            if (sequence == other.sequence) {
                return false;
            }
            int lowest_bit = (sequence ^ other.sequence)._Find_first();
            return sequence.test(lowest_bit);
        } else {
            return len < other.len;
        }
    }
};

State dp[MAX_N][MAX_N][MAX_BALANCE];


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        for (int j = 0; j < m; ++j) {
            grid[i][j] = s[j];
        }
    }
    const int max_balance = n / 2 + 5;
    // moves (if possible):
    // (i - 1; j - 1)
    // (i - 1; j)
    // (i - 1; j + 1)
    auto gen_moves = [&](int x, int y) -> vector<pair<int, int>> {
        vector<pair<int, int>> moves;
        if (y - 1 >= 0) {
            if (grid[x + 1][y - 1] != '*') {
                moves.emplace_back(x + 1, y - 1);
            }
        }
        if (y + 1 < m) {
            if (grid[x + 1][y + 1] != '*') {
                moves.emplace_back(x + 1, y + 1);
            }
        }
        if (grid[x + 1][y] != '*') {
            moves.emplace_back(x + 1, y);
        }
        return moves;
    };
    for (int j = 0; j < m; ++j) {
        if (grid[n - 1][j] == 'M') {
            dp[n - 1][j][0] = State(0);
        }
    }
    for (int i = n - 2; i >= 0; --i) {
        for (int j = 0; j < m; ++j) {
            auto moves = gen_moves(i, j);
            for (int k = 0; k < max_balance; ++k) {
                for (const auto& [prev_i, prev_j] : moves) {
                    State new_state;
                    if (grid[i][j] == '.' || grid[i][j] == '*') {
                        new_state = dp[prev_i][prev_j][k];
                    } else if (grid[i][j] == '(' && k != 0) {
                        new_state = dp[prev_i][prev_j][k - 1];
                        if (new_state.len == -INF) continue;
                        ++new_state.len;
                    } else if (grid[i][j] == ')' && k + 1 < max_balance) {
                        new_state = dp[prev_i][prev_j][k + 1];
                        if (new_state.len == -INF) continue;
                        new_state.sequence.set(new_state.len);
                        ++new_state.len;
                    }
                    dp[i][j][k] = max(dp[i][j][k], new_state);
                }
            }
        }
    }
    State max_state;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (i == 0 || grid[i][j] == '*') {
                max_state = max(max_state, dp[i][j][0]);
            }
        }
    }
    cout << max_state.len << '\n';
    for (int i = 0; i < max_state.len; ++i) {
        if (max_state.sequence.test(i)) {
            cout << ')';
        } else {
            cout << '(';
        }
    }
    cout << '\n';
    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Runtime error 216 ms 524288 KB Execution killed with signal 9
2 Runtime error 218 ms 524288 KB Execution killed with signal 9
3 Runtime error 261 ms 524288 KB Execution killed with signal 9
4 Runtime error 212 ms 524288 KB Execution killed with signal 9
5 Runtime error 226 ms 524288 KB Execution killed with signal 9
6 Runtime error 212 ms 524288 KB Execution killed with signal 9
7 Runtime error 208 ms 524288 KB Execution killed with signal 9
8 Runtime error 209 ms 524288 KB Execution killed with signal 9
9 Runtime error 228 ms 524288 KB Execution killed with signal 9
10 Runtime error 207 ms 524288 KB Execution killed with signal 9
11 Runtime error 209 ms 524288 KB Execution killed with signal 9
12 Runtime error 220 ms 524288 KB Execution killed with signal 9
13 Runtime error 223 ms 524288 KB Execution killed with signal 9
14 Runtime error 208 ms 524288 KB Execution killed with signal 9
15 Runtime error 221 ms 524288 KB Execution killed with signal 9
16 Runtime error 220 ms 524288 KB Execution killed with signal 9
17 Runtime error 215 ms 524288 KB Execution killed with signal 9
18 Runtime error 202 ms 524288 KB Execution killed with signal 9
19 Runtime error 205 ms 524288 KB Execution killed with signal 9
20 Runtime error 221 ms 524288 KB Execution killed with signal 9