#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()
#define sor(x) sort(all(x))
#define pi pair<int,int>
#define vi vector<int>
#define pb push_back
int N,M,Q;
void solve() {
cin >> N >> M >> Q;
vi bestfriend{0};
for (int i = 0; i < M;++i) {
int b = 0;
cin >> b;
bestfriend.pb(b + 1);
bestfriend.pb(b + N + 1);
}
auto Do = [&] (int a,int b) {
if (a > 2 * N)
a -= 2 * N;
if (a > b)
swap(a,b);
int ans = (int)1e9;
ans = b - a;
ans = min(ans,a + 2 * N - b);
return ans;
};
sor(bestfriend);
while(Q--) {
int a,b; cin >> a >> b;
++a,++b;
if (a > b)
swap(a,b);
int ans = (int)1e9;
ans = b - a;
ans = min(ans,a + 2 * N - b);
int l = 1,r = bestfriend.size() - 1;
int p = -1;
while (l <= r) {
int mid = (l + r) / 2;
//cout << mid << ' ' << bestfriend[mid] << '\n';
if (bestfriend[mid] <= a) {
p = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
if(p == -1) {
ans = min(ans,Do(bestfriend.back() - N,b) + a + 1 + 2 * N - bestfriend.back());
ans = min(ans,Do(bestfriend[1] + N,b) + bestfriend[1] - a + 1);
}
if(p != -1){
ans = min({ans,Do(bestfriend[p],b) + a - bestfriend[p],Do(bestfriend[p + 1],b) + bestfriend[p + 1] - a});
ans = min(ans,Do(N + bestfriend[p],b) + a - bestfriend[p] + 1);
ans = min(ans,Do(N + bestfriend[p + 1],b) + bestfriend[p + 1] - a + 1);
}
cout << ans << "\n";
}
}
main(){
solve();
}
Compilation message
Main.cpp:64:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
64 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
348 KB |
Output is correct |
2 |
Correct |
25 ms |
348 KB |
Output is correct |
3 |
Correct |
27 ms |
616 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
44 ms |
564 KB |
Output is correct |
6 |
Correct |
30 ms |
604 KB |
Output is correct |
7 |
Correct |
33 ms |
860 KB |
Output is correct |
8 |
Correct |
33 ms |
860 KB |
Output is correct |
9 |
Correct |
33 ms |
860 KB |
Output is correct |
10 |
Correct |
37 ms |
880 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
136 ms |
6652 KB |
Output is correct |
4 |
Correct |
68 ms |
2204 KB |
Output is correct |
5 |
Correct |
32 ms |
780 KB |
Output is correct |
6 |
Correct |
30 ms |
604 KB |
Output is correct |
7 |
Correct |
29 ms |
600 KB |
Output is correct |
8 |
Correct |
30 ms |
600 KB |
Output is correct |
9 |
Correct |
29 ms |
600 KB |
Output is correct |
10 |
Correct |
30 ms |
600 KB |
Output is correct |
11 |
Correct |
29 ms |
604 KB |
Output is correct |
12 |
Correct |
29 ms |
836 KB |
Output is correct |
13 |
Correct |
30 ms |
604 KB |
Output is correct |
14 |
Correct |
30 ms |
852 KB |
Output is correct |
15 |
Correct |
122 ms |
4444 KB |
Output is correct |
16 |
Correct |
95 ms |
4284 KB |
Output is correct |
17 |
Correct |
199 ms |
9656 KB |
Output is correct |
18 |
Correct |
190 ms |
9656 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
348 KB |
Output is correct |
2 |
Correct |
25 ms |
348 KB |
Output is correct |
3 |
Correct |
27 ms |
616 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
44 ms |
564 KB |
Output is correct |
6 |
Correct |
30 ms |
604 KB |
Output is correct |
7 |
Correct |
33 ms |
860 KB |
Output is correct |
8 |
Correct |
33 ms |
860 KB |
Output is correct |
9 |
Correct |
33 ms |
860 KB |
Output is correct |
10 |
Correct |
37 ms |
880 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |