#include <bits/stdc++.h> using namespace std; long long N; int M; long long S; long long ANS; long long NR; void update_ANS(long long l, long long r){ if(S <= l){ if(ANS > l - S){ ANS = l - S; NR = l; } } else if(S >= r){ if(ANS > S - r){ ANS = S - r; NR = r; } } } int main(){ cin.tie(NULL); ios_base::sync_with_stdio(0); cin >> N >> M >> S; long long l, r; vector<pair<long long, long long> > V; for(int i = 0; i < M; i++){ cin >> l >> r; if(l > r){ swap(l, r); } V.push_back(make_pair(l, r)); } sort(V.begin(), V.end()); ANS = N + 100; NR = 1; if(V[0].first > 1){ update_ANS(1, V[0].first - 1); } for(int i = 0; i < M - 1; i++){ if(V[i].second + 1 != V[i + 1].first){ update_ANS(V[i].second + 1, V[i + 1].first - 1); } } if(V.back().second < N){ update_ANS(V.back().second + 1, N); } cout << NR << endl; }
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #include <bits/stdc++.h> using namespace std; long long N; int M; long long S; long long ANS; long long NR; void update_ANS(long long l, long long r){ if(S <= l){ if(ANS > l - S){ ANS = l - S; NR = l; } } else if(S >= r){ if(ANS > S - r){ ANS = S - r; NR = r; } } } int main(){ cin.tie(NULL); ios_base::sync_with_stdio(0); cin >> N >> M >> S; long long l, r; vector<pair<long long, long long> > V; for(int i = 0; i < M; i++){ cin >> l >> r; if(l > r){ swap(l, r); } V.push_back(make_pair(l, r)); } sort(V.begin(), V.end()); ANS = N + 100; NR = 1; if(V[0].first > 1){ update_ANS(1, V[0].first - 1); } for(int i = 0; i < M - 1; i++){ if(V[i].second + 1 != V[i + 1].first){ update_ANS(V[i].second + 1, V[i + 1].first - 1); } } if(V.back().second < N){ update_ANS(V.back().second + 1, N); } cout << NR << endl; } |