結果

問題 No.1028 闇討ち
ユーザー QCFiumQCFium
提出日時 2020-04-17 21:40:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 121 ms / 2,000 ms
コード長 1,067 bytes
コンパイル時間 1,587 ms
コンパイル使用メモリ 168,164 KB
実行使用メモリ 16,168 KB
最終ジャッジ日時 2024-04-14 13:21:50
合計ジャッジ時間 3,924 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,944 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,944 KB
testcase_09 AC 7 ms
6,944 KB
testcase_10 AC 9 ms
6,940 KB
testcase_11 AC 30 ms
6,940 KB
testcase_12 AC 114 ms
15,724 KB
testcase_13 AC 114 ms
15,508 KB
testcase_14 AC 65 ms
11,904 KB
testcase_15 AC 22 ms
6,944 KB
testcase_16 AC 121 ms
16,128 KB
testcase_17 AC 120 ms
16,148 KB
testcase_18 AC 121 ms
16,080 KB
testcase_19 AC 121 ms
16,168 KB
testcase_20 AC 121 ms
16,168 KB
testcase_21 AC 121 ms
15,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

int main() {
	int n = ri();
	int a[n][n];
	for (auto &i : a) for (auto &j : i) j = ri() - 1;
	std::vector<std::pair<int, int> > cover[n];
	for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cover[a[i][j]].push_back({i, j});
	int64_t res = 0;
	for (int i = 0; i < n; i++) {
		int imos[n + 1];
		memset(imos, 0, sizeof(imos));
		for (auto j : cover[i]) {
			imos[0]++;
			imos[std::max(0, j.first - j.second)]--;
			imos[std::min(n, j.first + j.second + 1)]--;
			imos[n]++;
		}
		for (int j = 0; j < n; j++) imos[j + 1] += imos[j];
		int index = -1;
		for (int j = 0; j < n; j++) {
			if (j + 1 == n || imos[j] <= 0) {
				index = j;
				break;
			}
		}
		auto trial = [&] (int index) {
			if (index < 0) index = 0;
			if (index >= n) index = n - 1;
			int64_t r0 = 0;
			for (auto j : cover[i]) r0 += std::max(std::abs(j.first - index), j.second);
			return r0;
		};
		res += std::min({trial(index), trial(index - 1), trial(index + 1)});
	}
	printf("%" PRId64 "\n", res);
	return 0;
}
0