#include <bits/stdc++.h> using namespace std; #define int long long vector<pair<int,int>> v; unordered_map<int,bool> um; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n,m,s; cin >> n >> m >> s; for(int i = 0; i<m; i++){ int a,b; cin >> a >> b; v.emplace_back(a,b); um[a] = true; um[b] = true; } sort(v.begin(),v.end()); int best_left = 0; int best_right = LLONG_MAX; for(auto x : v){ int left = x.first - 1; int right = x.second + 1; if(left < s and !um[left]){ best_left = max(best_left, left); } if(right > s and !um[right]){ best_right = min(best_right, right); } } if(best_right > n){ best_right = LLONG_MAX; } if(best_left != 0 and s-best_left <= best_right-s){ cout << best_left <<"\n"; }else{ cout << best_right << "\n"; } }
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 | #include <bits/stdc++.h> using namespace std; #define int long long vector<pair<int,int>> v; unordered_map<int,bool> um; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n,m,s; cin >> n >> m >> s; for(int i = 0; i<m; i++){ int a,b; cin >> a >> b; v.emplace_back(a,b); um[a] = true; um[b] = true; } sort(v.begin(),v.end()); int best_left = 0; int best_right = LLONG_MAX; for(auto x : v){ int left = x.first - 1; int right = x.second + 1; if(left < s and !um[left]){ best_left = max(best_left, left); } if(right > s and !um[right]){ best_right = min(best_right, right); } } if(best_right > n){ best_right = LLONG_MAX; } if(best_left != 0 and s-best_left <= best_right-s){ cout << best_left <<"\n"; }else{ cout << best_right << "\n"; } } |