結果

問題 No.1028 闇討ち
ユーザー motmot
提出日時 2020-04-17 22:45:01
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 264 ms / 2,000 ms
コード長 2,217 bytes
コンパイル時間 748 ms
コンパイル使用メモリ 88,960 KB
実行使用メモリ 23,936 KB
最終ジャッジ日時 2024-04-14 15:00:56
合計ジャッジ時間 4,369 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 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,944 KB
testcase_09 AC 11 ms
6,944 KB
testcase_10 AC 15 ms
6,944 KB
testcase_11 AC 57 ms
7,936 KB
testcase_12 AC 238 ms
22,784 KB
testcase_13 AC 240 ms
22,656 KB
testcase_14 AC 131 ms
15,360 KB
testcase_15 AC 43 ms
7,296 KB
testcase_16 AC 254 ms
23,808 KB
testcase_17 AC 255 ms
23,680 KB
testcase_18 AC 255 ms
23,808 KB
testcase_19 AC 255 ms
23,936 KB
testcase_20 AC 264 ms
23,680 KB
testcase_21 AC 257 ms
23,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
const int inf=1e9+7;
const ll mod=1e9+7;

int main() {
    int N;
    cin>>N;
    vector<vector<pair<int, int> > >arr(N);
    int tmp;
    for(int i=0;i<N;++i){
        for(int j=0;j<N;++j){
            cin>>tmp;
            tmp--;
            arr[tmp].push_back(make_pair(i, j));
        }
    }
    int up[N][N+1];
    int down[N][N+1];
    int imosu[N][N];
    for(int i=0;i<N;++i){
        for(int j=0;j<N;++j){
            up[i][j] = 0;
            down[i][j] = 0;
            imosu[i][j] = 0;
        }
    }
    int ans = 0;
    for(int i=0;i<N;++i){
        for(int j=0;j<N;++j){
            ans += arr[i][j].se;
            if(arr[i][j].fi+arr[i][j].se>=N){
                down[i][N]++;
            }
            else{
                down[i][arr[i][j].fi+arr[i][j].se+1]++;
            }
            if(arr[i][j].fi-arr[i][j].se<0){
                up[i][0]++;
            }
            else{
                up[i][arr[i][j].fi-arr[i][j].se]++;
                imosu[i][0] += arr[i][j].fi-arr[i][j].se;
            }
        }
    }
    for(int i=0;i<N;++i){
        for(int j=1;j<N;++j){
            up[i][j] = up[i][j] + up[i][j-1];
            down[i][j] = down[i][j] + down[i][j-1];
        }
    }
    /**
    //cout<<endl;
    for(int i=0;i<N;++i){
        for(int j=0;j<N;++j){
            cout<<up[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl;
    for(int i=0;i<N;++i){
        for(int j=0;j<N;++j){
            cout<<down[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<endl;
    **/
    for(int i=0;i<N;++i){
        for(int j=1;j<N;++j){
            imosu[i][j] = imosu[i][j-1] - (N-up[i][j-1]) + down[i][j];
        }
    }
    int maximo;
    for(int i=0;i<N;++i){
        maximo = N*N;
        for(int j=0;j<N;++j){
            //cout<<imosu[i][j]<<" ";
            if(maximo>imosu[i][j])maximo = imosu[i][j];
        }
        //cout<<endl;
        ans += maximo;
    }
    //cout<<endl;
    cout<<ans<<endl;
}
0