結果
問題 | No.2743 Twisted Lattice |
ユーザー | shobonvip |
提出日時 | 2024-04-21 04:11:38 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 308 ms / 3,000 ms |
コード長 | 3,460 bytes |
コンパイル時間 | 5,175 ms |
コンパイル使用メモリ | 280,984 KB |
実行使用メモリ | 34,600 KB |
最終ジャッジ日時 | 2024-10-13 02:35:56 |
合計ジャッジ時間 | 8,571 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 228 ms
34,596 KB |
testcase_01 | AC | 308 ms
25,256 KB |
testcase_02 | AC | 295 ms
25,124 KB |
testcase_03 | AC | 241 ms
34,600 KB |
testcase_04 | AC | 283 ms
34,472 KB |
testcase_05 | AC | 278 ms
34,468 KB |
testcase_06 | AC | 292 ms
34,600 KB |
testcase_07 | AC | 288 ms
34,472 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 2 ms
5,248 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; //* ATCODER #include<atcoder/all> using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include<boost/multiprecision/cpp_int.hpp> using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template <typename T> bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template <typename T> bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template <typename T> T max(vector<T> &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template <typename T> T min(vector<T> &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template <typename T> T sum(vector<T> &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } // defcomp template <typename T> vector<T> compress(vector<T> &X) { vector<T> vals = X; sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); return vals; } // ----- // importbisect template <typename T> int bisect_left(vector<T> &X, T v){ return lower_bound(X.begin(), X.end(), v) - X.begin(); } template <typename T> int bisect_right(vector<T> &X, T v){ return upper_bound(X.begin(), X.end(), v) - X.begin(); } // ----- int main(){ ll h,w,n; cin >> h >> w >> n; vector<ll> a(n), b(n), ind(n); vector<tuple<ll,ll,int>> tar; rep(i,0,n){ cin >> a[i] >> b[i]; tar.push_back(make_tuple(b[i],a[i],i)); } sort(tar.begin(), tar.end()); rep(i,0,n){ a[i] = get<1>(tar[i]); b[i] = get<0>(tar[i]); ind[i] = get<2>(tar[i]); } vector<ll> bc = compress(b); auto get = [&](ll i) -> int { return bisect_left(bc, i); }; auto hav = [&](ll z) -> bool { int tar = bisect_left(bc, z); if (tar >= (int)bc.size() || bc[tar] != z) return false; return true; }; vector<ll> cost(n, 4e18); vector<set<ll>> st((int)bc.size()); rep(i,0,n){ st[get(b[i])].insert(a[i]); } int piv = 0; ll nwmin = 4e18; rep(i,0,n){ int x = get(b[i]); { auto itr = st[x].lower_bound(a[i]); if (itr != st[x].begin()){ itr--; ll val = *itr; chmin(cost[ind[i]], abs(a[i] - val)); } } { auto itr = st[x].upper_bound(a[i]); if (itr != st[x].end()){ ll val = *itr; chmin(cost[ind[i]], abs(a[i] - val)); } } for (ll y: {b[i]-1, b[i]+1}){ if (!hav(y)) continue; int z = get(y); { auto itr = st[z].upper_bound(a[i]); if (itr != st[z].begin()){ itr--; chmin(cost[ind[i]], a[i]); } } { auto itr = st[z].upper_bound(a[i]); if (itr != st[z].end()){ ll val = *itr; chmin(cost[ind[i]], val); } } } while (piv < n && b[piv] <= b[i] - 2){ chmin(nwmin, a[piv] - b[piv]); piv++; } // (1, b[i]-2) にいくのに ll geta = abs(1LL - a[i]) + 2LL; ll migiue_parameta = 1LL - (b[i] - 2LL); chmin(cost[ind[i]], (nwmin - migiue_parameta) + geta); } piv = n-1; nwmin = 4e18; rrep(i,0,n){ while(piv >= 0 && b[piv] >= b[i] + 2){ chmin(nwmin, a[piv] + b[piv]); piv--; } ll geta = abs(1LL - a[i]) + 2LL; ll migiue_parameta = 1LL + (b[i] + 2LL); chmin(cost[ind[i]], (nwmin - migiue_parameta) + geta); } rep(i,0,n){ cout << cost[i] << '\n'; } }