結果

問題 No.1028 闇討ち
ユーザー ShibuyapShibuyap
提出日時 2020-04-19 05:50:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 237 ms / 2,000 ms
コード長 1,471 bytes
コンパイル時間 1,738 ms
コンパイル使用メモリ 170,844 KB
実行使用メモリ 20,268 KB
最終ジャッジ日時 2024-04-14 22:50:32
合計ジャッジ時間 5,338 ms
ジャッジサーバーID
(参考情報)
judge1 / 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 3 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 10 ms
6,940 KB
testcase_10 AC 14 ms
6,940 KB
testcase_11 AC 52 ms
7,552 KB
testcase_12 AC 221 ms
19,312 KB
testcase_13 AC 218 ms
19,288 KB
testcase_14 AC 118 ms
13,824 KB
testcase_15 AC 39 ms
6,944 KB
testcase_16 AC 235 ms
19,992 KB
testcase_17 AC 236 ms
20,076 KB
testcase_18 AC 236 ms
20,176 KB
testcase_19 AC 236 ms
20,004 KB
testcase_20 AC 237 ms
20,268 KB
testcase_21 AC 236 ms
20,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("YES");}else{puts("NO");}
#define MAX_N 200005

int main() {
    int n;
    cin >> n;
    int a[n][n];
    rep(i,n)rep(j,n){scanf("%d", &a[i][j]); a[i][j]--;}

    int ans = 0;
    priority_queue<int, vector<int>, greater<int>> l[n];
    priority_queue<int> r[n];
    rep(i,n){
        rep(j,n){
            ans += j;
            l[a[i][j]].push(i+j+1);
            r[a[i][j]].push(i-j-1);
        }
    }

    int sum[n][n];
    rep(i,n)rep(j,n)sum[i][j] = 0;

    rep(i,n){
        int now = 0;
        int tmp = 0;
        rep(j,n){
            while(l[i].size() && l[i].top() <= j){
                now++;
                l[i].pop();
            }
            tmp += now;
            sum[i][j] += tmp;
        }
        now = 0;
        tmp = 0;
        drep(j,n){
            while(r[i].size() && r[i].top() >= j){
                now++;
                r[i].pop();
            }
            tmp += now;
            sum[i][j] += tmp;
        }
        int mi = 1001001001;
        rep(j,n)mi = min(mi, sum[i][j]);
        ans += mi;
    }

    /*
    rep(j,n){
        rep(i,n){
            cout << sum[i][j] << ' ';
        }
        cout << endl;
    }
    */

    cout << ans << endl;
    return 0;
}
 
 
0