#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
const int mod = 1e9;
void add(int& a, int b) {
a += b;
if(a > mod) {
a -= mod;
}
}
vector<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
int DistanceSum(int n, int *X, int *Y) {
int sum = 0;
vector<vector<int>> adj(n);
map<pair<int, int>, int> mp;
for(int i = 0; i < n; ++i) {
mp[{X[i], Y[i]}] = i;
}
for(int i = 0; i < n; ++i) {
for(auto[row, col] : dir) {
row += X[i];
col += Y[i];
auto it = mp.find({row, col});
if(it == mp.end()) continue;
adj[i].push_back(it->second);
adj[it->second].push_back(i);
}
}
const int INF = 1e9 + 5;
int ans = 0;
for(int i = 0; i < n; ++i) {
// start a bfs
vector<int> dist(n, INF);
queue<int> q;
q.push(i);
dist[i] = 0;
while(!q.empty()) {
int sz = q.size();
for(int i = 0; i < sz; ++i) {
int p = q.front(); q.pop();
for(int x : adj[p]) {
if(dist[x] != INF) continue;
dist[x] = dist[p] + 1;
q.push(x);
}
}
}
// dbg(dist)
for(int j = i + 1; j < n; ++j) {
add(ans, dist[j]);
// dbg(ans, dist[j])
}
}
return ans;
}
Compilation message
city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:21:6: warning: unused variable 'sum' [-Wunused-variable]
21 | int sum = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
452 KB |
Output is correct |
2 |
Correct |
23 ms |
340 KB |
Output is correct |
3 |
Correct |
53 ms |
500 KB |
Output is correct |
4 |
Correct |
56 ms |
516 KB |
Output is correct |
5 |
Correct |
98 ms |
596 KB |
Output is correct |
6 |
Correct |
99 ms |
568 KB |
Output is correct |
7 |
Correct |
105 ms |
596 KB |
Output is correct |
8 |
Correct |
104 ms |
620 KB |
Output is correct |
9 |
Correct |
100 ms |
624 KB |
Output is correct |
10 |
Correct |
100 ms |
616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1076 ms |
3256 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1067 ms |
3080 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |