結果
問題 | No.1028 闇討ち |
ユーザー |
![]() |
提出日時 | 2020-04-17 21:51:47 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 352 ms / 2,000 ms |
コード長 | 2,115 bytes |
コンパイル時間 | 1,107 ms |
コンパイル使用メモリ | 84,544 KB |
最終ジャッジ日時 | 2025-01-09 19:56:21 |
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
#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; }