제출 #1366429

#제출 시각아이디문제언어결과실행 시간메모리
1366429africCircle Passing (EGOI24_circlepassing)C++20
100 / 100
121 ms8844 KiB
#include <bits/stdc++.h>
using namespace std;

long long walkForward(long long x, long long y) {
    if (x > y) {
        swap(x,y);
    }
    return y-x;
}
long long walkBack(long long x, long long y, long long n) {
    if (x > y) {
        swap(x,y);
    }
    //cout << x + ((2*n)-y) << endl;
    return x + ((2*n)-y);
}

long long handle(long long p, long long x, long long y, long long n, bool end) {
    long long moves = 0;
    if (end) {
        // P is an endpoint close to y, so we need to enter its startpoint from x.
        // p's startpoint is p-n.
        moves += min(walkForward(x,p-n), walkBack(x,p-n,n));
        moves++; // one move from taking friend edge
        // we then need to get to y from the endpoint (p)
        moves += min(walkForward(p,y),walkBack(p,y,n));
    }
    else {
        // P is a startpoint close to y, so we need to enter its endpoint from x.
        // p's endpoint is p+n.
        moves += min(walkForward(x,p+n), walkBack(x,p+n,n));
        moves++; // one move from taking friend edge
        // we then need to enter y from the startpoint
        moves += min(walkForward(p,y), walkBack(p,y,n));
    }
    return moves;
}

int main() {
    long long n;
    int m, q;
    cin >> n >> m >> q;
    vector<long long> startPoint;
    vector<long long> endPoint;
    for (int i = 0; i < m; i++) {
        long long s;
        cin >> s;
        startPoint.push_back(s);
        endPoint.push_back(s+n);
    }
    vector<long long> ans;
    for (int i = 0; i < q; i++) {
        long long x, y;
        cin >> x >> y;
        if (x > y) {
            swap(x,y); // x is always smaller than y
        }
        long long forward = walkForward(x,y);
        long long backward = walkBack(x,y,n);
        auto end_left_it = lower_bound(endPoint.begin(),endPoint.end(),y);
        long long end_left;
        if (end_left_it == endPoint.begin() && *end_left_it != y) {
            end_left = endPoint.back();
        }
        else if (*end_left_it == y) {
            end_left = y;
        }
        else {
            end_left = *(end_left_it - 1);
        }
        long long start_left;
        auto start_left_it = lower_bound(startPoint.begin(),startPoint.end(),y);
        if (start_left_it == startPoint.begin() && *start_left_it != y) {
            start_left = startPoint.back();
        }
        else if (*start_left_it == y) {
            start_left = y;
        }
        else {
            start_left = *(start_left_it - 1);
        }
        long long end_right;
        auto end_right_it = upper_bound(endPoint.begin(),endPoint.end(),y);
        if (end_left ==y) {
            end_right = y;
        }
        else if (end_right_it == endPoint.end()) {
            end_right = endPoint[0];
        }
        else {
            end_right = *end_right_it;
        }
        long long start_right;
        auto start_right_it = upper_bound(startPoint.begin(),startPoint.end(),y);
        if (start_left ==y) {
            start_right = y;
        }
        else if (start_right_it == startPoint.end()) {
            start_right = startPoint[0];
        }
        else {
            start_right = *start_right_it;
        }
        /*cout << start_left << endl;
        cout << start_right << endl;
        cout << end_left << endl;
        cout << end_right << endl;*/
        vector<long long> options;
        options.push_back(handle(start_left, x, y,n,false));
        options.push_back(handle(start_right,x,y,n,false));
        options.push_back(handle(end_left,x,y,n,true));
        options.push_back(handle(end_right,x,y,n,true));
        options.push_back(forward);
        options.push_back(backward);
        ans.push_back(*min_element(options.begin(),options.end()));
    }
    for (long long i : ans) {
        cout << i << endl;
    }
    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…