結果

問題 No.2743 Twisted Lattice
ユーザー ponjuiceponjuice
提出日時 2024-04-18 00:27:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,029 ms / 3,000 ms
コード長 1,857 bytes
コンパイル時間 3,100 ms
コンパイル使用メモリ 218,036 KB
実行使用メモリ 36,936 KB
最終ジャッジ日時 2024-04-18 00:28:13
合計ジャッジ時間 12,014 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 845 ms
36,852 KB
testcase_01 AC 587 ms
18,176 KB
testcase_02 AC 582 ms
18,024 KB
testcase_03 AC 948 ms
36,864 KB
testcase_04 AC 1,029 ms
36,864 KB
testcase_05 AC 935 ms
36,860 KB
testcase_06 AC 883 ms
36,760 KB
testcase_07 AC 1,024 ms
36,936 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    ll h, w, n;
    cin >> h >> w >> n;
    vector<pair<ll,ll>> a(n);
    map<ll, set<ll>> as;

    for (int i = 0; i < n; i++) {
        cin >> a[i].second >> a[i].first;
        as[a[i].first].insert(a[i].second);
    }
    vector<ll> ans(n, 1e10);

    // case a[i].first == a[j].first
    for (int i = 0; i < n; i++) {
        auto itr = as[a[i].first].upper_bound(a[i].second);
        if (itr != as[a[i].first].end()) {
            ans[i] = min(ans[i], *itr - a[i].second);
        }

        itr = as[a[i].first].find(a[i].second);
        if (itr != as[a[i].first].begin()) {
            itr = prev(itr);
            ans[i] = min(ans[i], a[i].second - *itr);
        }
    }

    // case abs(a[i].first - a[j].first) == 1
    for (int i = 0; i < n; i++) {
        if (as.find(a[i].first - 1) != as.end()) {
            ans[i] = min(ans[i], max(a[i].second, *as[a[i].first - 1].begin()));
        }

        if (as.find(a[i].first + 1) != as.end()) {
            ans[i] = min(ans[i], max(a[i].second, *as[a[i].first + 1].begin()));
        }
    }

    // case abs(a[i].first - a[j].first) > 1
    vector<int> iot(n);
    iota(iot.begin(), iot.end(), 0);
    sort(iot.begin(), iot.end(), [&](int i, int j){
        return a[i] < a[j];
    });
    {// l to r
        ll b = 1e10;
        for (auto i : iot) {
            ans[i] = min(ans[i], a[i].first + a[i].second - 1 + b);
            b = min(b, a[i].second - 1 - a[i].first);
        }
    }
    {// r to l
        ll b = 1e10;
        reverse(iot.begin(), iot.end());
        for (auto i : iot) {
            ans[i] = min(ans[i], -a[i].first + a[i].second - 1 + b);
            b = min(b, a[i].second - 1 + a[i].first);
        }
    }

    
    for(int i = 0; i < n; i++){
        cout << ans[i] << endl;
    }
}
0