結果

問題 No.1028 闇討ち
ユーザー KoDKoD
提出日時 2020-04-17 21:51:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 312 ms / 2,000 ms
コード長 2,115 bytes
コンパイル時間 967 ms
コンパイル使用メモリ 88,724 KB
実行使用メモリ 11,392 KB
最終ジャッジ日時 2024-04-14 13:31:00
合計ジャッジ時間 5,056 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 12 ms
6,940 KB
testcase_10 AC 18 ms
6,940 KB
testcase_11 AC 69 ms
6,940 KB
testcase_12 AC 289 ms
11,008 KB
testcase_13 AC 287 ms
10,752 KB
testcase_14 AC 157 ms
7,552 KB
testcase_15 AC 51 ms
6,940 KB
testcase_16 AC 308 ms
11,392 KB
testcase_17 AC 309 ms
11,264 KB
testcase_18 AC 309 ms
11,264 KB
testcase_19 AC 308 ms
11,264 KB
testcase_20 AC 310 ms
11,264 KB
testcase_21 AC 312 ms
11,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_): l(l_), r(std::max(l_, r_)) { }
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int l_, int r_): l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

int main() {
  int N;
  std::cin >> N;
  std::vector<std::vector<std::pair<int, int>>> house(N);
  for (auto &vec: house) {
    vec.reserve(N);
  }
  for (int i: revrange(0, N)) {
    for (int j: range(0, N)) {
      int x;
      std::cin >> x;
      --x;
      house[x].emplace_back(i, j);
    }
  }
  long long sum = 0;
  for (int i: range(0, N)) {
    std::vector<int> points;
    points.reserve(2 * N);
    for (auto [x, y]: house[i]) {
      int l = std::max(0, x - y);
      int r = std::min(N - 1, x + y);
      points.push_back(l);
      points.push_back(r);
    }
    std::sort(points.begin(), points.end());
    int p = points[N - 1];
    int tmp = 0;
    for (auto [x, y]: house[i]) {
      sum += std::max(std::abs(x - p), y);
      tmp += std::max(std::abs(x - p), y);
    }
  }
  std::cout << sum << '\n';
  return 0;
}
0