結果

問題 No.2436 Min Diff Distance
ユーザー noya2noya2
提出日時 2023-08-13 08:50:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 472 ms / 2,000 ms
コード長 1,859 bytes
コンパイル時間 2,960 ms
コンパイル使用メモリ 215,912 KB
実行使用メモリ 34,836 KB
最終ジャッジ日時 2024-05-01 02:28:20
合計ジャッジ時間 10,661 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 452 ms
33,884 KB
testcase_04 AC 452 ms
33,956 KB
testcase_05 AC 451 ms
34,084 KB
testcase_06 AC 448 ms
33,956 KB
testcase_07 AC 472 ms
33,960 KB
testcase_08 AC 462 ms
34,084 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 254 ms
34,392 KB
testcase_12 AC 224 ms
34,836 KB
testcase_13 AC 278 ms
26,764 KB
testcase_14 AC 36 ms
7,048 KB
testcase_15 AC 432 ms
33,288 KB
testcase_16 AC 171 ms
18,004 KB
testcase_17 AC 382 ms
30,616 KB
testcase_18 AC 340 ms
28,884 KB
testcase_19 AC 381 ms
31,204 KB
testcase_20 AC 241 ms
21,124 KB
testcase_21 AC 92 ms
11,644 KB
testcase_22 AC 37 ms
7,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }

template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }

const ll linf = 1e15;

ll op(ll a, ll b){
    return min(a,b);
}
ll e(){
    return linf;
}

vector<ll> min_dist(int n, vector<ll> xs, vector<ll> ys){
    vector<vector<pair<ll,int>>> xys(n);
    for (int i = 0; i < n; i++){
        xys[xs[i]].emplace_back(ys[i],i);
    }
    atcoder::segtree<ll,op,e> lseg(n), rseg(n);
    vector<ll> ans(n,linf);
    for (ll x = 0; x < n; x++){
        sort(xys[x].begin(),xys[x].end());
        for (auto [y, i] : xys[x]){
            ll le = lseg.prod(0,y) + x + y;
            ll ri = rseg.prod(y,n) + x - y;
            chmin(ans[i],min(le,ri));
            lseg.set(y,-x-y);
            rseg.set(y,-x+y);
        }
    }
    return ans;
}

int main(){
    int n; cin >> n;
    ll pma = -linf, pmi = linf, mma = -linf, mmi = linf;
    vector<ll> xs(n), ys(n);
    for (int i = 0; i < n; i++){
        cin >> xs[i] >> ys[i];
        xs[i]--, ys[i]--;
        chmax(pma,xs[i]+ys[i]);
        chmin(pmi,xs[i]+ys[i]);
        chmax(mma,xs[i]-ys[i]);
        chmin(mmi,xs[i]-ys[i]);
    }
    vector<ll> mas(n,-linf), mis(n,linf);
    vector<ll> ul = min_dist(n,xs,ys);
    for (int i = 0; i < n; i++){
        ll p = xs[i]+ys[i];
        ll m = xs[i]-ys[i];
        chmax(mas[i],max(pma-p,p-pmi));
        chmax(mas[i],max(mma-m,m-mmi));
        chmin(mis[i],ul[i]);
        xs[i] = n-1-xs[i];
        ys[i] = n-1-ys[i];
    }
    vector<ll> dr = min_dist(n,xs,ys);
    ll ans = linf;
    for (int i = 0; i < n; i++){
        chmin(mis[i],dr[i]);
        chmin(ans,mas[i]-mis[i]);
    }
    cout << ans << endl;
}
0