This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int dist(int a, int b, int L)
{
return min(max(a, b) - min(a, b), L - max(a, b) + min(a, b));
}
int main()
{
int N, M, Q;
cin >> N >> M >> Q;
vector<int> Tab(M);
for (int i = 0; i < M; i++)
{
cin >> Tab[i];
}
for (int i = 0; i < M; i++)
Tab.push_back(Tab[i] + N);
for (int i = 0; i < 2 * M; i++)
Tab.push_back(Tab[i] + 2 * N);
for (int i = 0; i < Q; i++)
{
int x, y;
cin >> x >> y;
int rig = lower_bound(Tab.begin(), Tab.end(), x) - Tab.begin();
int lef = (rig - 1 + 2 * M) % (2 * M);
lef = Tab[lef], rig = Tab[rig] % (2 * N);
int ans = dist(x, y, 2 * N);
ans = min(ans, dist(x, rig, 2 * N) + 1 + dist(y, (rig + N) % (2 * N), 2 * N));
ans = min(ans, dist(lef, x, 2 * N) + 1 + dist((lef + N) % (2 * N), y, 2 * N));
cout << ans << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |