結果
| 問題 |
No.1028 闇討ち
|
| コンテスト | |
| ユーザー |
ta7uw
|
| 提出日時 | 2020-04-17 23:09:50 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 783 ms / 2,000 ms |
| コード長 | 1,850 bytes |
| コンパイル時間 | 1,752 ms |
| コンパイル使用メモリ | 177,480 KB |
| 実行使用メモリ | 28,800 KB |
| 最終ジャッジ日時 | 2024-10-03 15:08:08 |
| 合計ジャッジ時間 | 9,706 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 20 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG
/**
* For DEBUG
* https://github.com/ta7uw/cpp-pyprint
*/
#include "cpp-pyprint/pyprint.h"
#endif
void Main() {
ll N;
cin >> N;
vector<vector<ll>> grid(N, vector<ll>(N));
vector<ll> cnt(N, 0);
vector<vector<P>> point(N);
rep(i, N) {
rep(j, N) {
ll a;
cin >> a;
grid[i][j] = a;
cnt[a - 1] += i + 1;
point[a - 1].emplace_back(i + 1, j + 1);
}
}
ll ans = 0;
rep(t, N) {
ll h = cnt[t];
h /= N;
ll M = INF;
rep2(h2, h - 100, h + 100) {
ll ans2 = 0;
rep(j, N) {
P p = point[t][j];
if (p.first == h2) {
ans2 += p.second - 1;
} else if (p.second == 1) {
ans2 += abs(p.first - h2);
} else {
ll f = abs(1 - p.second);
ll s = abs(p.first - h2);
ll m = min(f, s);
ll add = m + abs(f - m) + abs(s - m);
ans2 += add;
}
}
M = min(M, ans2);
}
ans += M;
}
cout << ans << '\n';
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
Main();
return 0;
}
ta7uw