Submission #810509

# Submission time Handle Problem Language Result Execution time Memory
810509 2023-08-06T10:35:01 Z AkramElOmrani Ideal city (IOI12_city) C++17
0 / 100
269 ms 262144 KB
#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) {
	// map<pair<int, int>, int> mp;
	// for(int i = 0; i < n; ++i) {
	// 	mp[{X[i], Y[i]}] = i;
	// }
	vector<pair<int, int>> v;
	for(int i = 0; i < n; ++i) {
		v.emplace_back(X[i], Y[i]);
	}
	sort(v.begin(), v.end());
	const int INF = 1e9 + 5;
	int ops = 0;
	int ans = 0;
	vector<vector<int>> from(n, vector<int>(n));
	// O(n^2)?
	for(int i = 0; i < n; ++i) {
		// start a bfs
		vector<int> dist(n, INF);
		queue<int> q;
		auto pp = lower_bound(v.begin(), v.end(), make_pair(X[i], Y[i])) - v.begin();
		q.push(pp);
		dist[pp] = 0;
		while(!q.empty()) {
		  int sz = q.size();
			for(int i = 0; i < sz; ++i) {
			ops++;
			int p = q.front(); q.pop();
			for(auto[row, col] : dir) {
				row += X[p];
				col += Y[p];
				auto it = lower_bound(v.begin(), v.end(), make_pair(row, col));
				if(it == v.end() || *it != make_pair(row, col)) continue;
				int i = it - v.begin();
				if(dist[i] != INF) continue;
				dist[i] = dist[p] + 1;
				q.push(i);
			}
		}
	}
	for(int j = i + 1; j < n; ++j) {
		from[pp][j] = dist[j];
		from[j][pp] = dist[j];
	}
  }
  for(int i = 0; i < n; ++i) {
  	for(int j = i + 1; j < n; ++j) {
  		add(ans, from[i][j]);
  	}
  }
  assert(ops <= n * n);
  return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 269 ms 4260 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 89 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 85 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -