結果

問題 No.1028 闇討ち
ユーザー Y17Y17
提出日時 2020-04-17 22:33:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 267 ms / 2,000 ms
コード長 1,495 bytes
コンパイル時間 1,840 ms
コンパイル使用メモリ 170,792 KB
実行使用メモリ 20,992 KB
最終ジャッジ日時 2024-04-14 14:50:40
合計ジャッジ時間 5,184 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 11 ms
6,944 KB
testcase_10 AC 15 ms
6,944 KB
testcase_11 AC 59 ms
7,552 KB
testcase_12 AC 248 ms
20,480 KB
testcase_13 AC 248 ms
20,224 KB
testcase_14 AC 137 ms
15,616 KB
testcase_15 AC 43 ms
7,040 KB
testcase_16 AC 265 ms
20,992 KB
testcase_17 AC 267 ms
20,864 KB
testcase_18 AC 266 ms
20,736 KB
testcase_19 AC 267 ms
20,736 KB
testcase_20 AC 267 ms
20,864 KB
testcase_21 AC 266 ms
20,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define int long long
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

template <typename T>
struct cumulative_sum_linear {
    int n;
    vector<T> x, a, b;

    cumulative_sum_linear(int n_ = 0) : n(n_), x(n, 0), a(n + 1, 0), b(n + 1, 0) {}

    void add(int l, int r, T aa, T bb) {
        a[l] += aa;
        a[r] -= aa;
        a[l] -= bb * l;
        a[r] += bb * l;
        b[l] += bb;
        b[r] -= bb;
    }

    void exec() {
        for (int i = 0; i < n; ++i) {
            x[i] = a[i] + b[i] * i;
            a[i + 1] += a[i];
            b[i + 1] += b[i];
        }
    }

    T operator[](int i) const { return x[i]; }
};

signed main(){
	int n;
	cin >> n;

	vector<pair<int, int>> a[1010];
	for(int i = 0;i < n;i++){
		int b[1010];
		for(int j = 0;j < n;j++){
			cin >> b[i];
			a[b[i]-1].push_back({i, j});
		}
	}

	int ans = 0;
	for(int num = 0;num < n;num++){

		cumulative_sum_linear<int> imos(n);
		for(int x = 0;x < n;x++){
			int i = a[num][x].first;
			int d = a[num][x].second;
			int l = max(i-d, 0ll);
			int r = min(i+d, n-1ll);
			if(l-1 >= 0) imos.add(0ll, l, i, -1);
			imos.add(l, r+1, d, 0);
			if(r+1 < n) imos.add(r+1, n, d+1, 1);
		}

		imos.exec();

		int mi = LLONG_MAX;
		for(int i = 0;i < n;i++){
			chmin(mi, imos[i]);
		}

		ans += mi;
	}

	cout << ans << endl;

	return 0;
}
0