結果
問題 | No.1028 闇討ち |
ユーザー | theory_and_me |
提出日時 | 2020-04-17 22:16:42 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 166 ms / 2,000 ms |
コード長 | 3,437 bytes |
コンパイル時間 | 2,160 ms |
コンパイル使用メモリ | 217,780 KB |
実行使用メモリ | 32,640 KB |
最終ジャッジ日時 | 2024-10-03 13:39:21 |
合計ジャッジ時間 | 4,512 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | AC | 1 ms
6,820 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 1 ms
6,820 KB |
testcase_05 | AC | 2 ms
6,816 KB |
testcase_06 | AC | 2 ms
6,816 KB |
testcase_07 | AC | 2 ms
6,816 KB |
testcase_08 | AC | 2 ms
6,820 KB |
testcase_09 | AC | 6 ms
6,820 KB |
testcase_10 | AC | 8 ms
6,816 KB |
testcase_11 | AC | 28 ms
9,600 KB |
testcase_12 | AC | 131 ms
30,848 KB |
testcase_13 | AC | 132 ms
30,720 KB |
testcase_14 | AC | 69 ms
18,048 KB |
testcase_15 | AC | 21 ms
8,064 KB |
testcase_16 | AC | 147 ms
32,512 KB |
testcase_17 | AC | 166 ms
32,512 KB |
testcase_18 | AC | 141 ms
32,512 KB |
testcase_19 | AC | 150 ms
32,640 KB |
testcase_20 | AC | 144 ms
32,640 KB |
testcase_21 | AC | 146 ms
32,640 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; const ll mod = 1e9 + 7; //const ll mod = 998244353; #define REP(i,n) for(int i=0;i<(int)n;++i) //debug #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){ os << "(" << v.first << ", " << v.second << ")"; return os; } template<class T> ostream& operator << (ostream& os, const vector<T> v){ for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os; } template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){ for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os; } string num2bit(ll num, ll len){ string bit = ""; REP(i, len){ bit += char('0'+(num>>i & 1)); } return bit; } vector< vector< int > > grid_bfs(vector< string > &s, int sx, int sy) { const int vx[] = {0, 1, 0, -1, 1, 1, -1, -1}, vy[] = {1, 0, -1, 0, 1, -1, 1, -1}; vector< vector< int > > min_cost(s.size(), vector< int >(s[0].size(), -1)); queue< pair< int, int > > que; for(int i = 0; i < (int)s.size(); i++) { for(int j = 0; j < (int)s[i].size(); j++) { if(i == sx && j == sy) { que.emplace(i, j); min_cost[i][j] = 0; } } } while(!que.empty()) { auto p = que.front(); que.pop(); for(int i = 0; i < 8; i++) { int nx = p.first + vx[i], ny = p.second + vy[i]; if(nx < 0 || ny < 0 || ny >= (int)s[0].size() || nx >= (int)s.size()) continue; if(min_cost[nx][ny] != -1) continue; if(s[nx][ny] == '#') continue; min_cost[nx][ny] = min_cost[p.first][p.second] + 1; que.emplace(nx, ny); } } return min_cost; } int main(){ cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; vector<vector<ll>> A(N, vector<ll>(N, 0)); REP(i, N){ REP(j, N){ cin >> A[i][j]; A[i][j]--; } } vector<string> s(N); REP(i, N){ REP(j, N){ s[i].push_back('.'); } } auto dist0 = grid_bfs(s, 0, 0); //cout << dist0 << endl; vector<ll> dist(N, 0); vector<ll> res(N, 0); REP(i, N){ REP(j, N){ dist[A[i][j]] += dist0[i][j]; } } REP(i, N) res[i] = dist[i]; vector<vector<ll>> S(N, vector<ll>(N+1, 0)); REP(i, N){ REP(j, N){ if(i+j<N)S[A[i][j]][i+j+1]++; } } REP(i, N){ REP(j, N){ S[i][j+1] += S[i][j]; } } vector<vector<ll>> T(N, vector<ll>(N+1, 0)); REP(i, N){ REP(j, N){ if(i+j<N)T[A[N-1-i][j]][i+j+1]++; } } REP(i, N){ REP(j, N){ T[i][j+1] += T[i][j]; } } REP(i, N-1){ REP(j, N){ dist[j] += S[j][i+1] - S[j][0]; dist[j] -= T[j][N-1-i] - T[j][0]; chmin(res[j], dist[j]); } } ll ans = 0; REP(i, N) ans += res[i]; cout << ans << endl; return 0; }