Submission #1345092

#TimeUsernameProblemLanguageResultExecution timeMemory
1345092muhammad-ahmadCircle Passing (EGOI24_circlepassing)C++20
42 / 100
551 ms1114112 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

int n, m, q; 

int dist(int x, int y){
	int ans;
	if (x < y) ans = min(y - x, x + 2 * n - y);
	else ans = min(x - y, y + 2 * n - x);
	return ans;
}

signed main(){
	cin >> n >> m >> q;
	bool f[2 * n] = {};
	for (int i = 1; i <= m; i++){
		int k; cin >> k;
		f[k] = f[k + n] = 1;
	}	
	int lst = -1;
	int l[2 * n], r[2 * n];
	for (int i = 0; i < 6 * n; i++){
		if (f[i % (2 * n)]) lst = i % (2 * n);
		r[i % (2 * n)] = lst;
	}
	lst = -1;
	for (int i = 6 * n - 1; i >= 0; i--){
		if (f[i % (2 * n)]) lst = i % (2 * n);
		l[i % (2 * n)] = lst;
	}
	
	for (int Q = 1; Q <= q; Q++){
		int x, y; cin >> x >> y;
		int ans = dist(x, y);
		ans = min(ans, dist(l[x], x) + dist((l[x] + n) % (2 * n), y) + 1);
		ans = min(ans, dist(r[x], x) + dist((r[x] + n) % (2 * n), y) + 1);
		cout << ans << endl;
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...