Submission #1100257

# Submission time Handle Problem Language Result Execution time Memory
1100257 2024-10-13T10:59:17 Z vjudge1 Maxcomp (info1cup18_maxcomp) C++17
0 / 100
500 ms 504 KB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <limits>

using namespace std;

int n, m;
vector<vector<int>> mat;

struct C {
    int x, y;
};

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

bool valid(int x, int y) {
    return x >= 0 && x < n && y >= 0 && y < m;
}

bool connected(const vector<C>& sub) {
    if (sub.empty()) return false;

    vector<vector<bool>> vis(n, vector<bool>(m, false));
    queue<C> q;

    q.push(sub[0]);
    vis[sub[0].x][sub[0].y] = true;
    int cnt = 1;

    while (!q.empty()) {
        C curr = q.front();
        q.pop();

        for (int d = 0; d < 4; ++d) {
            int nx = curr.x + dx[d];
            int ny = curr.y + dy[d];

            if (valid(nx, ny) && !vis[nx][ny]) {
                for (const auto& c : sub) {
                    if (c.x == nx && c.y == ny) {
                        vis[nx][ny] = true;
                        q.push({nx, ny});
                        cnt++;
                        break;
                    }
                }
            }
        }
    }

    return cnt == static_cast<int>(sub.size());
}

int weight(const vector<C>& sub) {
    int maxV = numeric_limits<int>::min(), minV = numeric_limits<int>::max();
    for (const auto& c : sub) {
        maxV = max(maxV, mat[c.x][c.y]);
        minV = min(minV, mat[c.x][c.y]);
    }

    return maxV - minV - static_cast<int>(sub.size());
}

int main() {
    cin >> n >> m;
    mat.resize(n, vector<int>(m));

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> mat[i][j];
        }
    }

    int maxW = numeric_limits<int>::min();
    int totalCells = n * m;

    for (int mask = 1; mask < (1 << totalCells); ++mask) {
        vector<C> sub;

        for (int i = 0; i < totalCells; ++i) {
            if (mask & (1 << i)) {
                int x = i / m;
                int y = i % m;
                sub.push_back({x, y});
            }
        }

        if (connected(sub)) {
            int w = weight(sub);
            maxW = max(maxW, w);
        }
    }

    cout << maxW << endl;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 386 ms 336 KB Output is correct
2 Execution timed out 504 ms 504 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 336 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 386 ms 336 KB Output is correct
2 Execution timed out 504 ms 504 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 386 ms 336 KB Output is correct
2 Execution timed out 504 ms 504 KB Time limit exceeded
3 Halted 0 ms 0 KB -