結果

問題 No.2731 Two Colors
ユーザー ooaiu
提出日時 2024-08-10 14:33:32
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 3,348 bytes
コンパイル時間 5,241 ms
コンパイル使用メモリ 292,708 KB
最終ジャッジ日時 2025-02-23 22:22:16
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other TLE * 1 -- * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

// #undef LOCAL

#ifdef ONLINE_JUDGE
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif

#ifdef LOCAL
// #define _GLIBCXX_DEBUG
#include "../algo/debug.hpp"
#else
#define debug(...) void(0)
#endif

#include <bits/stdc++.h>
using namespace std;

#if __has_include(<atcoder/all>)
#include <atcoder/all>
// using namespace atcoder;
#endif

constexpr int inf = (1<<30) - 1;
constexpr long long linf = (1LL<<60) - 1;
using ll = long long;
using ld = long double;
#define len(x) int((x).size())
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define LB(c, x) distance(begin(c), lower_bound(all(c), x))
#define UB(c, x) distance(begin(c), upper_bound(all(c), x))
template <typename F> ll binary_search(F check, ll ok, ll ng, bool CHECK = true) { if(CHECK) assert(check(ok)); while(abs(ok - ng) > 1) { ll mi = (ok + ng) / 2; (check(mi) ? ok : ng) = mi; } return ok; }
template <typename T, typename S> auto min(const T& a, const S& b) { return(a < b ? a : b); }
template <typename T, typename S> auto max(const T& a, const S& b) { return(a > b ? a : b); }
template <typename T, typename S> inline bool chmax(T& a, const S& b) { return(a < b ? a = b, 1 : 0); }
template <typename T, typename S> inline bool chmin(T& a, const S& b) { return(a > b ? a = b, 1 : 0); }
// ------------------------------------------------------------------


constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
void solve() {
    int h, w; cin >> h >> w;
    vector<vector<int>> a(h, vector<int>(w));
    for(int i = 0; i < h; i++) {
        for(int j = 0; j < w; j++) {
            cin >> a[i][j];
        }
    }
    vector<bool> d1(h*w, false), d2(h*w, false);
    using type = pair<int, int>;
    priority_queue<type, vector<type>, greater<type>> pq1, pq2;
    pq1.push({0, 0});
    pq2.push({0, h * w - 1});
    auto isin = [&](int i, int j) -> bool { return(0 <= i && i < h && 0 <= j && j < w); };
    auto idx = [&](int i, int j) -> int { return i * w + j; };
    bool f = true;
    while(!pq1.empty() && !pq2.empty()) {
        bool g = false;
        if(f) {
            auto[_, id] = pq1.top(); pq1.pop();
            d1[id] = true;
            int i = id/w, j = id%w;
            for(int r = 0; r < 4; r++) {
                int ii = i + dx[r], jj = j + dy[r];
                if(!isin(ii, jj) || d1[idx(ii, jj)]) continue;
                if(d2[idx(ii, jj)]) g = true;
                pq1.push({a[ii][jj], idx(ii, jj)});
            }
            if(g) break;
        }else {
            auto[_, id] = pq2.top(); pq2.pop();
            d2[id] = true;
            int i = id/w, j = id%w;
            for(int r = 0; r < 4; r++) {
                int ii = i + dx[r], jj = j + dy[r];
                if(!isin(ii, jj) || d2[idx(ii, jj)]) continue;
                if(d1[idx(ii, jj)]) g = true;
                pq2.push({a[ii][jj], idx(ii, jj)});
            }
            if(g) break;
        }
        f ^= 1;
    }
    int ans = 0;
    for(int i = 0; i < h; i++) {
        for(int j = 0; j < w; j++) {
            if(d1[idx(i, j)] || d2[idx(i, j)]) ans++;
        }
    }
    cout << ans - 2 << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    // fixed(cout).precision(12);
    int T = 1;
    // cin >> T;
    while(T--) {
        solve();
    }
}
0