結果

問題 No.1028 闇討ち
ユーザー theory_and_me
提出日時 2020-04-17 22:16:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 233 ms / 2,000 ms
コード長 3,437 bytes
コンパイル時間 3,095 ms
コンパイル使用メモリ 209,504 KB
最終ジャッジ日時 2025-01-09 20:12:28
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0