結果

問題 No.1028 闇討ち
ユーザー tsutajtsutaj
提出日時 2020-04-17 22:01:32
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 1,808 bytes
コンパイル時間 1,312 ms
コンパイル使用メモリ 108,248 KB
実行使用メモリ 19,072 KB
最終ジャッジ日時 2024-04-14 14:10:50
合計ジャッジ時間 3,912 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 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 6 ms
6,940 KB
testcase_10 AC 8 ms
6,940 KB
testcase_11 AC 32 ms
7,168 KB
testcase_12 AC 172 ms
18,048 KB
testcase_13 AC 171 ms
17,920 KB
testcase_14 AC 81 ms
11,520 KB
testcase_15 AC 22 ms
6,944 KB
testcase_16 AC 187 ms
18,944 KB
testcase_17 AC 187 ms
19,072 KB
testcase_18 AC 188 ms
19,072 KB
testcase_19 AC 186 ms
19,072 KB
testcase_20 AC 189 ms
19,072 KB
testcase_21 AC 188 ms
19,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

int dist(int a, int b, int x, int y) {
    return max(abs(a - x), abs(b - y));
}

int main() {
    // 距離: min(a-x, b-y)
    int N; scanf("%d", &N);

    vector<ll> sum(N), acc(N), ans(N, LONGINF);
    vector< vector<ll> > inc(N, vector<ll>(N)), dec(N, vector<ll>(N));
    for(int i=0; i<N; i++) {
        for(int j=0; j<N; j++) {
            int v; scanf("%d", &v); v--;
            int d = dist(0, 0, i, j);
            int p1 = max(i - j, 0);
            int p2 = min(i + j, N - 1);
            sum[v] += d;
            inc[p1][v]++;
            inc[p2][v]++;
            dec[0][v]++;
        }
    }

    for(int r=0; r<N; r++) {
        for(int v=0; v<N; v++) {
            chmin(ans[v], sum[v]);
            
            acc[v] += inc[r][v] - dec[r][v];
            sum[v] += acc[v];
        }
    }

    ll res = accumulate(ans.begin(), ans.end(), 0LL);
    cout << res << endl;
    return 0;
}
0