#include<bits/stdc++.h> #define fi first #define se second #define pb push_back #define int long long using namespace std; int32_t main(){ cin.tie(0)->sync_with_stdio(0); int n, m, s; cin >> n >> m >> s; vector<pair<int, int>> seg; vector<int> tryy; for(int i = 0; i < m; i++){ int l, p; cin >> l >> p; seg.pb({l, p}); int cnt = l-1; if(cnt >= 1 && cnt <= n) tryy.pb(cnt); cnt = p+1; if(cnt >= 1 && cnt <= n) tryy.pb(cnt); } int mini = LLONG_MAX; int idx = LLONG_MAX; for(auto w : tryy){ int is = 0; for(auto [l, p] : seg){ if(w >= l && w <= p) is = 1; } if(is) continue; if(abs(s-w) < mini){ mini = abs(s-w); idx = w; }else if(abs(s-w)==mini){ if(w < idx) idx = w; } } cout << idx << "\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> #define fi first #define se second #define pb push_back #define int long long using namespace std; int32_t main(){ cin.tie(0)->sync_with_stdio(0); int n, m, s; cin >> n >> m >> s; vector<pair<int, int>> seg; vector<int> tryy; for(int i = 0; i < m; i++){ int l, p; cin >> l >> p; seg.pb({l, p}); int cnt = l-1; if(cnt >= 1 && cnt <= n) tryy.pb(cnt); cnt = p+1; if(cnt >= 1 && cnt <= n) tryy.pb(cnt); } int mini = LLONG_MAX; int idx = LLONG_MAX; for(auto w : tryy){ int is = 0; for(auto [l, p] : seg){ if(w >= l && w <= p) is = 1; } if(is) continue; if(abs(s-w) < mini){ mini = abs(s-w); idx = w; }else if(abs(s-w)==mini){ if(w < idx) idx = w; } } cout << idx << "\n"; } |