#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
int DistanceSum(int n, int x[], int y[]) {
map<pair<int, int>, int> mp;
for (int i = 0; i < n; i++) {
mp[make_pair(x[i], y[i])] = i;
}
vector<vector<int>> g(n);
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
for (int i = 0; i < n; i++) {
for (int dir = 0; dir < 4; dir++) {
int nx = x[i] + dx[dir];
int ny = y[i] + dy[dir];
if (mp.count(make_pair(nx, ny))) {
g[i].emplace_back(mp[make_pair(nx, ny)]);
}
}
}
const long long mod = (int) 2e9;
long long res = 0;
for (int r = 0; r < n; r++) {
vector<int> d(n, -1);
queue<int> que;
d[r] = 0;
que.emplace(r);
while (!que.empty()) {
int v = que.front();
que.pop();
for (int to : g[v]) {
if (d[to] == -1) {
d[to] = d[v] + 1;
que.emplace(to);
}
}
}
for (int i = r + 1; i < n; i++) {
res += d[i];
res %= mod;
}
}
return (int) (res / 2);
}
#ifdef tabr
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n = 11;
int x[] = {2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5};
int y[] = {5, 6, 3, 6, 3, 4, 5, 6, 3, 4, 6};
cout << DistanceSum(n, x, y) << '\n';
return 0;
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
27 ms |
432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1075 ms |
3004 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1083 ms |
3020 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |