답안 #948929

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
948929 2024-03-18T16:41:25 Z Nhoksocqt1 화성 (APIO22_mars) C++17
0 / 100
1 ms 1456 KB
#include<bits/stdc++.h>
using namespace std;

#define inf 0x3f3f3f3f
#define sz(x) int((x).size())
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;

template<class X, class Y>
	inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
	inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

const int MAXN = 22;
const int lx[] = {-1, 0, 0, 1}, ly[] = {0, -1, 1, 0};

vector<ii> B[MAXN][2 * MAXN][2 * MAXN];
int sz[MAXN][2 * MAXN][2 * MAXN];
bool dx[2 * MAXN][2 * MAXN], dxc[MAXN][2 * MAXN][2 * MAXN][3][3];

void bfs_mark(int x, int y, int n) {
    queue<ii> qu;
    qu.push(ii(x, y));
    dx[x][y] = 1;

    while(sz(qu)) {
        int x(qu.front().fi), y(qu.front().se);
        qu.pop();

        for (int id = 0; id < 4; ++id) {
            int u(x + lx[id]), v(y + ly[id]);
            if(min(u, v) < 0 || max(u, v) > 2 * n || dx[u][v])
                continue;

            dx[u][v] = 1;
            qu.push(ii(u, v));
        }
    }
}

void init(int n, int maxk) {
    for (int i = 0; i <= 2 * n; ++i) {
        for (int j = 0; j <= 2 * n; ++j) {
            for (int k = 0; k <= maxk + 1; ++k) {
                sz[k][i][j] = 0;
                for (int lx = 0; lx < 3; ++lx) {
                    for (int ly = 0; ly < 3; ++ly)
                        dxc[k][i][j][lx][ly] = 0;
                }

                B[k][i][j].clear();
            }

            B[0][i][j].push_back(ii(i, j));
            sz[0][i][j] = 1;
        }
    }

    for (int k = 0; k <= maxk; ++k) {
        for (int i = 0; i + 2 * k <= 2 * n; ++i) {
            for (int j = 0; j + 2 * k <= 2 * n; ++j) {
                int minSz(1e9), u(-1), v(-1);
                for (int lx = 0; lx < 3; ++lx) {
                    for (int ly = 0; ly < 3; ++ly) {
                        int ni(i - lx), nj(j - ly);
                        if(min(ni, nj) >= 0 && max(ni, nj) <= 2 * (n - k - 1) && minSz > sz[k + 1][ni][nj]) {
                            minSz = sz[k + 1][ni][nj];
                            u = ni, v = nj;
                        }
                    }
                }

                dxc[k + 1][u][v][i - u][j - v] = 1;
                sz[k + 1][u][v] += sz[k][i][j];
                //cout << '.' << u << ' ' << v << ' ' << i << ' ' << j << '\n';
            }
        }

        if(maxk + 1 < n)
            continue;

        for (int i = 0; i + 2 * (k + 1) <= 2 * n; ++i) {
            for (int j = 0; j + 2 * (k + 1) <= 2 * n; ++j) {
                for (int lx = 0; lx < 3; ++lx) {
                    for (int ly = 0; ly < 3; ++ly) {
                        int ni(i + lx), nj(j + ly);
                        if(dxc[k + 1][i][j][lx][ly]) {
                            for (int it = 0; it < sz(B[k][ni][nj]); ++it)
                                B[k + 1][i][j].push_back(B[k][ni][nj][it]);
                        }
                    }
                }
            }
        }
    }
}

string process(vector<vector<string>> str, int i, int j, int k, int n) {
    init(n, k);

    string res;
    for (int lx = 0; lx < 3; ++lx) {
        for (int ly = 0; ly < 3; ++ly) {
            if(dxc[k + 1][i][j][lx][ly]) {
                string s(str[lx][ly]);
                int szn = sz[k][i + lx][j + ly];
                while(sz(s) > szn)
                    s.pop_back();

                res += s;
            }
        }
    }

    if(k + 1 == n) {
        for (int it = 0; it < sz(B[n][0][0]); ++it) {
            int x(B[n][0][0][it].fi), y(B[n][0][0][it].se);
            dx[x][y] = (res[it] == '1');
        }

        /*for (int i = 0; i <= 2 * n; ++i) {
            for (int j = 0; j <= 2 * n; ++j)
                cout << dx[i][j] << " \n"[j == 2 * n];
        }*/

        int cnt_islands(0);
        for (int i = 0; i <= 2 * n; ++i) {
            for (int j = 0; j <= 2 * n; ++j) {
                if(!dx[i][j]) {
                    bfs_mark(i, j, n);
                    ++cnt_islands;
                }
            }
        }

        string ans;
        while(cnt_islands > 0) {
            ans.push_back(cnt_islands % 2 + '0');
            cnt_islands /= 2;
        }

        if(sz(ans) < 100)
            ans.insert(sz(ans), 100 - sz(ans), '0');

        return ans;
    }

    if(sz(res) < 100)
        res.insert(sz(res), 100 - sz(res), '0');

    return res;
}

#ifdef Nhoksocqt1

int main(void) {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    #define TASK "mars"
    if(fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }

    vector<vector<int>> A;
    int n;
    cin >> n;

    for (int i = 0; i < 2 * n + 1; ++i) {
        vector<int> p;
        for (int j = 0; j < 2 * n + 1; ++j) {
            int z;
            cin >> z;
            p.push_back(z);
        }

        A.push_back(p);
    }

    string new_h[2 * MAXN][2 * MAXN], h[2 * MAXN][2 * MAXN];
    for (int i = 0; i < 2 * n + 1; ++i) {
        for (int j = 0; j < 2 * n + 1; ++j) {
            h[i][j] = A[i][j] + '0';
            h[i][j].insert(1, 99, '0');
        }
    }

    for (int i = 0; i < n; ++i) {
        for (int x = 0; x + 2 * (i + 1) <= 2 * n; ++x) {
            for (int y = 0; y + 2 * (i + 1) <= 2 * n; ++y) {
                vector<vector<string>> p;
                for (int lx = 0; lx < 3; ++lx) {
                    vector<string> pt;
                    for (int ly = 0; ly < 3; ++ly)
                        pt.push_back(h[x + lx][y + ly]);

                    p.push_back(pt);
                }

                new_h[x][y] = process(p, x, y, i, n);
                cout << x << ' ' << y << ": " << new_h[x][y] << '\n';
            }
        }

        for (int x = 0; x <= 2 * n; ++x) {
            for (int y = 0; y <= 2 * n; ++y)
                h[x][y] = new_h[x][y];
        }
    }

    cout << "ANSWER: " << h[0][0] << '\n';

    int res(0);
    for (int it = 0; it < 30; ++it)
        res += (1LL << it) * (h[0][0][it] == '1');

    cout << "NUM ISLAND: " << res << '\n';

    return 0;
}

#endif // Nhoksocqt1
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 1456 KB Incorrect
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 1456 KB Incorrect
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 1456 KB Incorrect
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 1456 KB Incorrect
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 1456 KB Incorrect
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 1456 KB Incorrect
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 1456 KB Incorrect
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 1456 KB Incorrect
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 1456 KB Incorrect
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 1456 KB Incorrect
2 Halted 0 ms 0 KB -