Submission #1361332

#TimeUsernameProblemLanguageResultExecution timeMemory
1361332biserailievaCircle Passing (EGOI24_circlepassing)C++20
100 / 100
160 ms24096 KiB
#include <bits/stdc++.h>
using namespace std;

int n;

int dist_circle(int x, int y)
{
    int d = abs(x - y);            
    return min(d, 2*n - d);         
}

void try_teleport(int &ans, int x, int y, int z)
{
    int op1 = dist_circle(x, z) + 1 + dist_circle(z + n, y);
    int op2 = dist_circle(y, z) + 1 + dist_circle(z + n, x);
    ans = min(ans, min(op1, op2));
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int m, q;
    cin >> n >> m >> q;
    set<int> tele; 
    for(int i = 0; i < m; i++)
    {
        int k;
        cin >> k;
        tele.insert(k);
    }
    while(q--)
    {
        int x, y;
        cin >> x >> y;
        int ans = dist_circle(x, y); 
        if(y < x) swap(x, y);
        if(x >= n)
        {
            x -= n;
            y = (y + n) % (2*n);
        }
        auto it1 = tele.lower_bound(x);
        auto it2 = tele.lower_bound(y % n);
        if(it1 == tele.end()) it1 = tele.begin();
        if(it2 == tele.end()) it2 = tele.begin();
        try_teleport(ans, x, y, *it1);
        try_teleport(ans, x, y, *it2);
        if(it1 == tele.begin()) it1 = tele.end();
        if(it2 == tele.begin()) it2 = tele.end();
        it1--;
        it2--;
        try_teleport(ans, x, y, *it1);
        try_teleport(ans, x, y, *it2);
        cout << ans << '\n';
    }
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...