結果

問題 No.2897 2集合間距離
ユーザー shkiiii_shkiiii_
提出日時 2024-09-21 02:56:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,295 bytes
コンパイル時間 2,612 ms
コンパイル使用メモリ 212,064 KB
実行使用メモリ 15,852 KB
最終ジャッジ日時 2024-09-21 02:56:14
合計ジャッジ時間 10,624 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
15,852 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 10 ms
5,376 KB
testcase_15 AC 18 ms
5,376 KB
testcase_16 AC 366 ms
9,516 KB
testcase_17 AC 402 ms
9,432 KB
testcase_18 AC 369 ms
9,576 KB
testcase_19 AC 368 ms
9,456 KB
testcase_20 AC 396 ms
9,576 KB
testcase_21 AC 523 ms
9,452 KB
testcase_22 TLE -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}


// using ld = long double;
const int inf = 100000000;
// verified https://onlinejudge.u-aizu.ac.jp/status/users/shkii/submissions/1/CGL_5_A/judge/9351284/C++17
// vはfirstについてsortされている必要がある
// vはfirstについてsortされている必要がある
// vはfirstについてsortされている必要がある
inline int calc_closest_pair(vector<pair<pair<int,int>,int>> &v,int l,int r){
  if(r-l <= 1)return inf;
  int mid = (l+r)/2;
  int mid_x = v[mid].first.first;
  int d = min(calc_closest_pair(v,l,mid),calc_closest_pair(v,mid,r));
  sort(v.begin()+l,v.begin()+r,[](pair<pair<int,int>,int> a,pair<pair<int,int>,int> b){return a.first.second < b.first.second;});
  vector<pair<pair<int,int>,int>> w;//直線mid_xから距離d未満の列挙
  for(int i = l;i < r;i++){
    if(abs(v[i].first.first-mid_x) < d){
      for(int j = (int)(w.size())-1;j >= 0;j--){//実はこのループは高々5回程度しか回らない
        int dx = v[i].first.first-w[j].first.first;
        int dy = v[i].first.second-w[j].first.second;
        if(dy >= d)break;
        if(v[i].second != w[j].second)d = min(d,abs(dx) + abs(dy));
      }
      w.emplace_back(v[i]);
    }
  }
  return d;
}
// vはfirstについてsortされている必要がある
// vはfirstについてsortされている必要がある

int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  vector<pair<pair<int,int>,int>> v;
  int n;cin >> n;
  rep(i,n){
    int a,b;cin >> a >> b;
    v.emplace_back(pair<pair<int,int>,int>{{a,b},0});
  }
  int m;cin >> m;
  rep(i,m){
    int a,b;cin >> a >> b;
    v.emplace_back(pair<pair<int,int>,int>{{a,b},1});
  }
  sort(ALL(v));
  cout << calc_closest_pair(v,0,sz(v)) << "\n";
  

  return 0;
}
0