結果

問題 No.1028 闇討ち
ユーザー betrue12betrue12
提出日時 2020-04-17 22:27:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 935 bytes
コンパイル時間 2,234 ms
コンパイル使用メモリ 202,212 KB
実行使用メモリ 12,288 KB
最終ジャッジ日時 2024-04-14 14:43:46
合計ジャッジ時間 4,628 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,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 7 ms
6,944 KB
testcase_10 AC 9 ms
6,944 KB
testcase_11 AC 31 ms
6,940 KB
testcase_12 AC 126 ms
11,904 KB
testcase_13 AC 125 ms
11,904 KB
testcase_14 AC 69 ms
9,728 KB
testcase_15 AC 24 ms
6,944 KB
testcase_16 AC 132 ms
12,160 KB
testcase_17 AC 134 ms
12,288 KB
testcase_18 AC 132 ms
12,160 KB
testcase_19 AC 131 ms
12,160 KB
testcase_20 AC 131 ms
12,160 KB
testcase_21 AC 135 ms
12,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main(){
    int N;
    cin >> N;
    vector<pair<int, int>> pos[1000];
    for(int i=0; i<N; i++) for(int j=0; j<N; j++){
        int a;
        scanf("%d", &a);
        pos[a-1].emplace_back(i, j);
    }

    int64_t ans = 0;
    for(int a=0; a<N; a++){
        vector<int> cost(N);
        vector<int> sl(N), sr(N);
        for(auto [i, j] : pos[a]){
            if(i-j-1>=0) sl[i-j-1]++;
            if(i+j+1<N)  sr[i+j+1]++;
        }
        int sum = 0, num = 0;
        for(int i=0; i<N; i++){
            num += sr[i];
            sum += num;
            cost[i] += sum;
        }
        sum = 0, num = 0;
        for(int i=N-1; i>=0; i--){
            num += sl[i];
            sum += num;
            cost[i] += sum;
        }
        ans += *min_element(cost.begin(), cost.end());
    }
    
    for(int j=0; j<N; j++) ans += j*N;
    cout << ans << endl;
    return 0;
}
0