#include<bits/stdc++.h> using namespace std; pair<long long, long long> przedzialy[1009]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, s, l, r; cin >> n >> m >> s; for(int i=1; i<=m; i++) { cin >> l >> r; przedzialy[i]={l, r}; } sort(przedzialy+1, przedzialy+1+m); int gdzie=1; for(int i=1; i<=m; i++) { if(przedzialy[i].second>=s) { gdzie=i; break; } } long long lewo=-1, prawo=-1; for(int i=gdzie-1; i>=1; i--) { if(przedzialy[i].second+1 != przedzialy[i+1].first) { lewo=przedzialy[i+1].first-1; break; } } if(lewo==-1) { if(przedzialy[1].first!=1) { lewo=przedzialy[1].first-1; } } for(int i=gdzie+1; i<=m; i++) { if(przedzialy[i].first-1 != przedzialy[i-1].second) { prawo=przedzialy[i-1].second+1; break; } } //cout << gdzie << ' ' << prawo << endl; if(prawo==-1) { if(przedzialy[m].second!=n) { prawo=przedzialy[m].second+1; } } //cout << lewo << ' ' << prawo << endl; if(lewo==-1) { cout << prawo; return 0; } if(prawo==-1) { cout << lewo; return 0; } if(abs(prawo-s) < abs(lewo-s)) { cout<<prawo; return 0; } cout << lewo; 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include<bits/stdc++.h> using namespace std; pair<long long, long long> przedzialy[1009]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, s, l, r; cin >> n >> m >> s; for(int i=1; i<=m; i++) { cin >> l >> r; przedzialy[i]={l, r}; } sort(przedzialy+1, przedzialy+1+m); int gdzie=1; for(int i=1; i<=m; i++) { if(przedzialy[i].second>=s) { gdzie=i; break; } } long long lewo=-1, prawo=-1; for(int i=gdzie-1; i>=1; i--) { if(przedzialy[i].second+1 != przedzialy[i+1].first) { lewo=przedzialy[i+1].first-1; break; } } if(lewo==-1) { if(przedzialy[1].first!=1) { lewo=przedzialy[1].first-1; } } for(int i=gdzie+1; i<=m; i++) { if(przedzialy[i].first-1 != przedzialy[i-1].second) { prawo=przedzialy[i-1].second+1; break; } } //cout << gdzie << ' ' << prawo << endl; if(prawo==-1) { if(przedzialy[m].second!=n) { prawo=przedzialy[m].second+1; } } //cout << lewo << ' ' << prawo << endl; if(lewo==-1) { cout << prawo; return 0; } if(prawo==-1) { cout << lewo; return 0; } if(abs(prawo-s) < abs(lewo-s)) { cout<<prawo; return 0; } cout << lewo; return 0; } |