結果

問題 No.1028 闇討ち
ユーザー ThistleThistle
提出日時 2020-03-28 18:27:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 1,080 bytes
コンパイル時間 507 ms
コンパイル使用メモリ 68,072 KB
実行使用メモリ 19,600 KB
最終ジャッジ日時 2023-09-15 23:11:22
合計ジャッジ時間 2,716 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 6 ms
6,228 KB
testcase_10 AC 8 ms
6,336 KB
testcase_11 AC 24 ms
10,560 KB
testcase_12 AC 92 ms
19,320 KB
testcase_13 AC 90 ms
19,436 KB
testcase_14 AC 51 ms
14,964 KB
testcase_15 AC 18 ms
10,484 KB
testcase_16 AC 97 ms
19,340 KB
testcase_17 AC 98 ms
19,264 KB
testcase_18 AC 98 ms
19,600 KB
testcase_19 AC 98 ms
19,388 KB
testcase_20 AC 97 ms
19,328 KB
testcase_21 AC 98 ms
19,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<algorithm>

using namespace std;
using ll = int_fast64_t;
#define int ll
#define rep(i,n) for(int (i) = 0 ; (i) < (n) ; (i)++)
#define rng(i,s,n) for(int (i) = (s) ; (i) < (n) ; (i)++)
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; }
ll read() { ll u, k = scanf("%lld", &u); return u; }

int n;
int a[2000][2000];
int b[2000];//現在のコスト
int ans[2000];//答えのコスト
int c[2000][2];
void solve() {
	cin >> n;
	rep(i, n)rep(j, n) {
		a[i][j] = read() - 1;
		b[a[i][j]] += i + j - min(i, j);
		if (j < i) c[a[i][j]][1]++;
	}
	rep(i, n) {
		ans[i] = b[i];
	}
	rep(i, n) {
		for (int j = 0; i - j >= 0; j++) {
			c[a[i - j][j]][0]++;
		}
		rep(j, n) {
			b[j] += c[j][0] - c[j][1];
		}
		rep(j, n) chmin(ans[j], b[j]);
		for (int j = 0; i + j + 1 < n; j++) {
			c[a[i + j + 1][j]][1]--;
		}
	}
	int sum = 0;
	rep(i, n) sum += ans[i];
	cout << sum << endl;
}
signed main() {
	solve();
}
0