結果

問題 No.1028 闇討ち
ユーザー micheeeeell1001
提出日時 2020-04-17 22:24:22
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,727 bytes
コンパイル時間 3,560 ms
コンパイル使用メモリ 239,392 KB
最終ジャッジ日時 2025-01-09 20:18:13
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 3 WA * 6 TLE * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
#define rep(i,x) for(ll i = 0; i < (ll)(x); i++)
#define rrep(i,x) for(ll i = (ll)(x)-1;0 <= i; i--)
#define reps(i,x) for(ll i = 1; i < (ll)(x)+1; i++)
#define rreps(i,x) for(ll i = (ll)(x); 1 <= i; i--)
#define debug(x) cerr << #x << ": " << (x) << "\n";
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> Pll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
const ll INF = numeric_limits<ll>::max()/4;
const int n_max = 1e5+10;
#define int ll

template<class T>
bool chmax(T &a, T b){if(a < b){a = b; return true;} return false;}
template<class T>
bool chmin(T &a, T b){if(a > b){a = b; return true;} return false;}

signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    ll n; cin >> n;
    vector graph(n, vector<ll>(n));
    rep(i,n)rep(j,n) cin >> graph[i][j];
    vector num(n, vector<Pll>());
    vector<ll> ave(n, 0LL);
    rep(i,n)rep(j,n){
        num[--graph[i][j]].emplace_back(Pll(i, j));
        ave[graph[i][j]] += i;
    }
    rep(i,n)ave[i] /= n;

    vector ans(n, vector<ll>(n, INF));
    rep(i,n){
        rep(k, n){
            for(int j = -2; j <= 2; j++){
                ll temp = 0;
                if(j + ave[k] < 0 || j + ave[k] > n)continue;
                for(auto &p : num[k])temp += max(p.second, abs(p.first - j - ave[k]));
                chmin(ans[i][k], temp);
            }
        }
    }

    ll ret = 0;
    rep(k,n){
        ll temp = INF;
        rep(i,n){
            chmin(temp, ans[i][k]);
        }
        ret += temp;
        
    }
    cout << ret << endl;
}
0