結果

問題 No.2436 Min Diff Distance
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-12-17 20:20:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 210 ms / 2,000 ms
コード長 3,153 bytes
コンパイル時間 1,239 ms
コンパイル使用メモリ 123,408 KB
実行使用メモリ 11,300 KB
最終ジャッジ日時 2023-12-17 20:20:50
合計ジャッジ時間 5,003 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 196 ms
11,300 KB
testcase_04 AC 196 ms
11,300 KB
testcase_05 AC 197 ms
11,300 KB
testcase_06 AC 210 ms
11,300 KB
testcase_07 AC 199 ms
11,300 KB
testcase_08 AC 197 ms
11,300 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 1 ms
6,676 KB
testcase_11 AC 93 ms
11,300 KB
testcase_12 AC 82 ms
11,300 KB
testcase_13 AC 130 ms
8,788 KB
testcase_14 AC 22 ms
6,676 KB
testcase_15 AC 195 ms
11,028 KB
testcase_16 AC 91 ms
7,188 KB
testcase_17 AC 164 ms
10,092 KB
testcase_18 AC 149 ms
9,476 KB
testcase_19 AC 172 ms
10,340 KB
testcase_20 AC 116 ms
8,348 KB
testcase_21 AC 51 ms
6,676 KB
testcase_22 AC 23 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


constexpr int INF = 1001001001;
void bChmax(vector<int> &bit, int pos, int val) {
  const int bitN = bit.size();
  for (int x = pos; x < bitN; x |= x + 1) chmax(bit[x], val);
}
int bMax(const vector<int> &bit, int pos) {
  int ret = -INF;
  for (int x = pos; x > 0; x &= x - 1) chmax(ret, bit[x - 1]);
  return ret;
}


int N;
vector<int> X, Y;

int main() {
  for (; ~scanf("%d", &N); ) {
    X.resize(N);
    Y.resize(N);
    for (int i = 0; i < N; ++i) {
      scanf("%d%d", &X[i], &Y[i]);
      --X[i];
      --Y[i];
    }
    
    vector<int> A(N), B(N);
    for (int i = 0; i < N; ++i) {
      A[i] = X[i] + Y[i];
      B[i] = X[i] - Y[i];
    }
    const int minA = *min_element(A.begin(), A.end());
    const int maxA = *max_element(A.begin(), A.end());
    const int minB = *min_element(B.begin(), B.end());
    const int maxB = *max_element(B.begin(), B.end());
    
    vector<int> ansMax(N);
    for (int i = 0; i < N; ++i) {
      ansMax[i] = max({A[i] - minA, maxA - A[i], B[i] - minB, maxB - B[i]});
    }
    
    vector<int> ansMin(N, INF);
    for (int dir = 0; dir < 4; ++dir) {
      // <= X[i], <= Y[i]
      vector<pair<pair<int, int>, int>> ps(N);
      for (int i = 0; i < N; ++i) {
        ps[i] = make_pair(make_pair(X[i], Y[i]), i);
      }
      sort(ps.begin(), ps.end());
      vector<int> bit(N, -INF);
      for (const auto &p : ps) {
        const int i = p.second;
        const int res = bMax(bit, Y[i] + 1);
        chmin(ansMin[i], X[i] + Y[i] - res);
        bChmax(bit, Y[i], X[i] + Y[i]);
      }
      
      if (dir & 1) {
        for (int i = 0; i < N; ++i) X[i] = N - 1 - X[i];
      } else {
        for (int i = 0; i < N; ++i) Y[i] = N - 1 - Y[i];
      }
    }
    
cerr<<"ansMax = "<<ansMax<<endl;
cerr<<"ansMin = "<<ansMin<<endl;
    int ans = INF;
    for (int i = 0; i < N; ++i) {
      chmin(ans, ansMax[i] - ansMin[i]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
0