結果

問題 No.1028 闇討ち
ユーザー KKT89KKT89
提出日時 2020-04-17 22:38:18
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 1,924 bytes
コンパイル時間 1,141 ms
コンパイル使用メモリ 108,480 KB
実行使用メモリ 20,532 KB
最終ジャッジ日時 2024-04-14 14:56:10
合計ジャッジ時間 4,395 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 3 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,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 10 ms
7,080 KB
testcase_10 AC 14 ms
11,084 KB
testcase_11 AC 48 ms
12,628 KB
testcase_12 AC 197 ms
19,648 KB
testcase_13 AC 198 ms
19,636 KB
testcase_14 AC 108 ms
16,560 KB
testcase_15 AC 36 ms
9,256 KB
testcase_16 AC 211 ms
20,276 KB
testcase_17 AC 212 ms
20,432 KB
testcase_18 AC 211 ms
20,532 KB
testcase_19 AC 213 ms
20,280 KB
testcase_20 AC 213 ms
20,156 KB
testcase_21 AC 213 ms
20,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <numeric>
#include <deque>
using namespace std;
typedef long long int ll;

int a[1001][1001];
int aa[1001][1001];
vector<pair<int,int>> v[1001];
vector<int> l[1001];
vector<int> r[1001];
int rr[1001];

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    ll res=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin >> aa[i][j];
            aa[i][j]--;
            a[n-1-j][n-1-i]=aa[i][j];
        }
    }

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            //cin >> a[i][j];
            //a[i][j]--;
            int ii=n-1-i;
            res+=ii;
            l[a[i][j]].push_back(j-ii);
            r[a[i][j]].push_back(j+ii);
            rr[a[i][j]]+=max(0,j-ii);
        }
    }
    for(int i=0;i<n;i++){
        sort(l[i].begin(),l[i].end());
        sort(r[i].begin(),r[i].end());
    }
    for(int i=0;i<n;i++){
        int mi=rr[i];
        int nowl=0;
        int nowr=0;
        int leftnow=0;
        int rightnow=n;
        int leftcost=0;
        int rightcost=rr[i];
        while(nowl<n&&l[i][nowl]<=0){
            nowl++;
            rightnow--;
        }
        while(nowr<n&&r[i][nowr]<=0){
            nowr++;
            leftnow++;
        }
        for(int j=1;j<n;j++){
            //printf("%d %d %d\n",mi,leftcost,rightcost );
            leftcost+=leftnow;
            rightcost-=rightnow;
            while(nowl<n&&l[i][nowl]<=j){
                nowl++;
                rightnow--;
            }
            while(nowr<n&&r[i][nowr]<=j){
                leftnow++;
                nowr++;
            }
            mi=min(mi,leftcost+rightcost);
        }
        //printf("%d %d %d\n",mi,leftcost,rightcost);
        //printf("\n");
        res+=mi;
    }
    printf("%lld\n",res);
}
0