結果

問題 No.1028 闇討ち
コンテスト
ユーザー betrue12
提出日時 2020-04-17 22:27:03
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 935 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,368 ms
コンパイル使用メモリ 219,936 KB
実行使用メモリ 12,544 KB
最終ジャッジ日時 2026-06-09 14:25:54
合計ジャッジ時間 3,137 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#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