Submission #1229141

#TimeUsernameProblemLanguageResultExecution timeMemory
1229141pumkinheadCircle Passing (EGOI24_circlepassing)C++20
100 / 100
64 ms4640 KiB
#include <bits/stdc++.h>
using namespace std;
int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    int n, m, q;
    cin>>n>>m>>q;
    vector<int> friends;
    for(int i=0; i<m; i++){
        int k;
        cin>>k;
        friends.push_back(k);
        friends.push_back(n+k);
    }
    sort(friends.begin(), friends.end());
    auto getDist = [&](int x, int y){
        if(x > y){
            swap(x, y);
        }
        return min(y-x, 2*n-y+x);
    };
    auto getOpposite = [&](int x){
        if(x < n){
            return x + n;
        }else{
            return x - n;
        }
    };
    for(int i=0; i<q; i++){
        int x, y;
        cin>>x>>y;
        if(x > y) swap(x, y);
        int ans = getDist(x, y);
        auto ptr = lower_bound(friends.begin(), friends.end(), x);
        int friendRight = (ptr == friends.end()) ? *friends.begin() : *ptr;
        ans = min(ans, getDist(x, friendRight) + getDist(getOpposite(friendRight), y) + 1);
        int friendLeft = (ptr == friends.begin()) ? *friends.rbegin() : *(ptr-1);
        ans = min(ans, getDist(x, friendLeft) + getDist(getOpposite(friendLeft), y) + 1);
        cout<<ans<<"\n";
    }
}
#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...