結果

問題 No.1028 闇討ち
ユーザー leaf_1415leaf_1415
提出日時 2020-04-17 21:54:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 535 ms / 2,000 ms
コード長 2,223 bytes
コンパイル時間 930 ms
コンパイル使用メモリ 84,376 KB
実行使用メモリ 71,292 KB
最終ジャッジ日時 2024-04-14 14:04:15
合計ジャッジ時間 6,906 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
29,924 KB
testcase_01 AC 11 ms
30,192 KB
testcase_02 AC 11 ms
30,072 KB
testcase_03 AC 11 ms
30,052 KB
testcase_04 AC 11 ms
32,104 KB
testcase_05 AC 12 ms
34,176 KB
testcase_06 AC 12 ms
30,420 KB
testcase_07 AC 12 ms
30,128 KB
testcase_08 AC 12 ms
30,188 KB
testcase_09 AC 21 ms
36,168 KB
testcase_10 AC 27 ms
38,180 KB
testcase_11 AC 85 ms
48,400 KB
testcase_12 AC 485 ms
69,764 KB
testcase_13 AC 485 ms
69,568 KB
testcase_14 AC 216 ms
59,108 KB
testcase_15 AC 61 ms
44,512 KB
testcase_16 AC 523 ms
71,164 KB
testcase_17 AC 517 ms
71,172 KB
testcase_18 AC 526 ms
71,284 KB
testcase_19 AC 527 ms
71,280 KB
testcase_20 AC 525 ms
71,284 KB
testcase_21 AC 535 ms
71,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<llint, llint> P;

llint n;
llint a[1005][1005];
vector<llint> vec[1005][1005];
llint d[1005][1005];
llint s[1005][1005];

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	for(int y = 1; y <= n; y++){
		for(int x = 1; x <= n; x++){
			cin >> a[x][y];
		}
	}
	
	
	for(int y = 1; y <= n; y++){
		for(int x = 1; x <= n; x++){
			if(y+x-1 <= n) vec[a[x][y]][y+x-1].push_back(x-1);
		}
	}
	for(int i = 1; i <= n; i++){
		llint sum = 0, cnt = 0;
		for(int y = 1; y <= n; y++){
			s[i][y] += sum;
			for(int j = 0; j < vec[i][y].size(); j++){
				llint x = vec[i][y][j];
				sum += x;
				cnt++;
			}
			sum += cnt;
		}
	}
	
	
	for(int y = 1; y <= n; y++){
		for(int x = 1; x <= n; x++){
			vec[x][y].clear();
		}
	}
	for(int y = 1; y <= n; y++){
		for(int x = 1; x <= n; x++){
			if(y-(x-1) >= 1) vec[a[x][y]][y-(x-1)].push_back(x-1);
		}
	}
	for(int i = 1; i <= n; i++){
		llint sum = 0, cnt = 0;
		for(int y = n; y >= 1; y--){
			s[i][y] += sum;
			for(int j = 0; j < vec[i][y].size(); j++){
				llint x = vec[i][y][j];
				sum += x;
				cnt++;
			}
			sum += cnt;
		}
	}
	
	
	for(llint y = 1; y <= n; y++){
		for(llint x = 1; x <= n; x++){
			llint id = a[x][y];
			llint l = max(1LL, y-(x-1)), r = min(n, y+(x-1));
			if(l > r) continue;
			d[id][l] += x-1, d[id][r+1] -= x-1;
		}
	}
	for(int i = 1; i <= n; i++){
		for(int y = 1; y <= n; y++) d[i][y] += d[i][y-1];
	}
	
	for(int i = 1; i <= n; i++){
		for(int y = 1; y <= n; y++) s[i][y] += d[i][y];
	}
	
	
	llint ans = 0;
	for(int i = 1; i <= n; i++){
		llint mn = inf;
		for(int y = 1; y <= n; y++) mn = min(mn, s[i][y]);
		ans += mn;
	}
	cout << ans << endl;
	
	return 0;
}
0