제출 #1339702

#제출 시각아이디문제언어결과실행 시간메모리
1339702sallyCircle Passing (EGOI24_circlepassing)C++20
100 / 100
181 ms4656 KiB
/*
1. 偶數個人
2. 人多,邊少
3. 站在對角線
*/

#include<iostream>
#include<vector>
#include<set>
#include<queue>
using namespace std;

typedef pair<int,int> pii;
int N, M, Q, total; // total=>總人數
vector<int> cl, ccl; // clockwise(1,2,3), counterclockwise(-3,-2,-1)
int dist(int s, int t) {
    return min(abs(s-t), total-abs(s-t));
}
int op(int x) {
    if(x<N) return x+N;
    return x-N;
}
int main() {
    cin>>N>>M>>Q;
    total = 2*N;
    for(int i=0; i<M; i++) {
        int k; cin>>k;
        cl.push_back(k);
    }
    for(int i=0; i<M; i++) cl.push_back(cl[i]+N);
    //vector<int> temp = cl;
    //for(int x: temp) cl.push_back(x+total);
    //for(int i=cl.size()-1; i>=0; i--) ccl.push_back(-cl[i]);
    while(Q--) {
        int s, t;
        cin>>s>>t;
        int ans = dist(s, t);
        /*for(int k: temp) {
            if(k>=2*N) k-=2*N;
            ans = min(ans, dist(s, k)+1+dist(op(k), t));
        }*/
        if(cl.size()==0) {cout<<ans<<'\n'; continue;}
        int i = lower_bound(cl.begin(), cl.end(), s) - cl.begin();
        int i1 = i%cl.size();
        int i2 = (i-1+cl.size())%cl.size();
        int j = lower_bound(cl.begin(), cl.end(), t) - cl.begin();
        int j1 = j%cl.size();
        int j2 = (j-1+cl.size())%cl.size();
       // cout<<cl[i1]<<' '<<cl[i2]<<' '<<cl[j1]<<' '<<cl[j2]<<'\n';
        ans = min(ans, dist(s, cl[i1])+1+dist(op(cl[i1]), t));
        ans = min(ans, dist(s, cl[i2])+1+dist(op(cl[i2]), t));
        ans = min(ans, dist(t, cl[j1])+1+dist(op(cl[j1]), s));
        ans = min(ans, dist(t, cl[j2])+1+dist(op(cl[j2]), s));
        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...