結果

問題 No.2731 Two Colors
ユーザー warner1129warner1129
提出日時 2024-04-19 21:53:50
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 165 ms / 3,000 ms
コード長 2,691 bytes
コンパイル時間 3,710 ms
コンパイル使用メモリ 298,064 KB
実行使用メモリ 15,104 KB
最終ジャッジ日時 2024-04-19 21:53:59
合計ジャッジ時間 7,393 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 162 ms
15,104 KB
testcase_01 AC 165 ms
14,976 KB
testcase_02 AC 162 ms
15,104 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 10 ms
5,376 KB
testcase_05 AC 6 ms
5,376 KB
testcase_06 AC 24 ms
5,376 KB
testcase_07 AC 27 ms
5,812 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 97 ms
11,500 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 97 ms
10,260 KB
testcase_12 AC 94 ms
11,352 KB
testcase_13 AC 81 ms
9,200 KB
testcase_14 AC 43 ms
7,168 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 39 ms
6,528 KB
testcase_17 AC 55 ms
7,552 KB
testcase_18 AC 10 ms
5,376 KB
testcase_19 AC 40 ms
6,204 KB
testcase_20 AC 61 ms
8,740 KB
testcase_21 AC 11 ms
5,376 KB
testcase_22 AC 100 ms
11,268 KB
testcase_23 AC 24 ms
5,376 KB
testcase_24 AC 13 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 80 ms
9,848 KB
testcase_27 AC 98 ms
11,576 KB
testcase_28 AC 61 ms
8,524 KB
testcase_29 AC 20 ms
5,376 KB
testcase_30 AC 35 ms
6,016 KB
testcase_31 AC 22 ms
5,376 KB
testcase_32 AC 122 ms
12,464 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template <class F, class S>
ostream &operator<<(ostream &s, const pair<F, S> &v) {
    s << "(" << v.first << ", " << v.second << ")";
    return s;
}
template <ranges::range T,
          class = enable_if_t<!is_convertible_v<T, string_view>>>
istream &operator>>(istream &s, T &&v) {
    for (auto &&x : v) s >> x;
    return s;
}
template <ranges::range T,
          class = enable_if_t<!is_convertible_v<T, string_view>>>
ostream &operator<<(ostream &s, T &&v) {
    for (auto &&x : v) s << x << ' ';
    return s;
}

#ifdef LOCAL
template <class... T> void dbg(T... x) {
    char e{};
    ((cerr << e << x, e = ' '), ...);
}
#define debug(x...) dbg(#x, '=', x, '\n')
#else
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define debug(...) ((void)0)
#endif

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define ff first
#define ss second

template <class T> inline constexpr T inf = numeric_limits<T>::max() / 2;
template <class T> bool chmin(T &a, T b) { return (b < a and (a = b, true)); }
template <class T> bool chmax(T &a, T b) { return (a < b and (a = b, true)); }

using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128;

constexpr i64 mod = 998244353;

void solve() {
    int n, m;
    cin >> n >> m;

    vector A(n, vector<int>(m));
    cin >> A;


    vector vis(n, vector<int>(m, -1));
    vector inque(n, vector<int>(m, 0));

    priority_queue<array<int, 3>> pq[2]{};
    pq[0].push({-A[0][0], 0, 0});
    pq[1].push({-A[n - 1][m - 1], n - 1, m - 1});
    inque[0][0] = 1;
    inque[n - 1][m - 1] = 2;
    
    const vector<pair<int, int>> dir{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    auto safe = [&](int x, int y) -> bool {
        return 0 <= x and x < n and 0 <= y and y < m;
    };

    int ans = 0;
    for (int cur = 0; ; cur ^= 1) {
        ans++;
        auto [_, x, y] = pq[cur].top();
        pq[cur].pop();

        vis[x][y] = cur;

        for (auto [dx, dy] : dir) {
            int nx = x + dx;
            int ny = y + dy;
            if (safe(nx, ny)) {
                if (vis[nx][ny] == (cur ^ 1)) {
                    cout << ans - 2 << '\n';
                    return;
                }
                if (~inque[nx][ny] >> cur & 1) {
                    inque[nx][ny] |= (1 << cur);
                    pq[cur].push({-A[nx][ny], nx, ny});
                }
            }
        }
    }
}

signed main() {
    cin.tie(0)->sync_with_stdio(false);
    cin.exceptions(cin.failbit);

    int t = 1;
    // cin >> t;

    while (t--) {
        solve();
    }
    
    return 0;
}




0