#include <bits/stdc++.h> using namespace std; #define ll long long #define f first #define s second int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll N, P, S, ans, r=9999999999999; cin >> N >> P >> S; vector<pair<ll, ll>> vp={{0, 0}}; while (P--){ ll a, b; cin >> a >> b; if (a>b) swap(a, b); vp.push_back({a, b}); } vp.push_back({N+1, N+1}); sort(vp.begin(), vp.end()); for (int i=1; i<vp.size(); i++){ // cout << endl; // cout << vp[i].f << " " << vp[i].s << endl; ll p=vp[i-1].s+1, k=vp[i].f-1; // cout << p << " " << k << endl; if (p>k) continue; if (S>k && abs(S-k)<r){ r=abs(S-k); ans=k; } if (S<k && abs(S-p)<r){ r=abs(S-p); ans=p; } } cout << ans; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <bits/stdc++.h> using namespace std; #define ll long long #define f first #define s second int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll N, P, S, ans, r=9999999999999; cin >> N >> P >> S; vector<pair<ll, ll>> vp={{0, 0}}; while (P--){ ll a, b; cin >> a >> b; if (a>b) swap(a, b); vp.push_back({a, b}); } vp.push_back({N+1, N+1}); sort(vp.begin(), vp.end()); for (int i=1; i<vp.size(); i++){ // cout << endl; // cout << vp[i].f << " " << vp[i].s << endl; ll p=vp[i-1].s+1, k=vp[i].f-1; // cout << p << " " << k << endl; if (p>k) continue; if (S>k && abs(S-k)<r){ r=abs(S-k); ans=k; } if (S<k && abs(S-p)<r){ r=abs(S-p); ans=p; } } cout << ans; return 0; } |