#include <bits/stdc++.h> using namespace std; pair<long long, long long> tab[1001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, s, min_odl = LLONG_MAX, wynik = -1;; int m; cin >> n >> m >> s; for (int i = 0; i < m; i++) { cin >> tab[i].first >> tab[i].second; } sort(tab, tab + m); if (tab[0].first > 1) { long long akt = tab[0].first - 1; long long akt_odl = abs(s - akt); if (akt_odl < min_odl || (akt_odl == min_odl && akt < wynik)) { min_odl = akt_odl; wynik = akt; } } for (int i = 1; i < m; i++) { if (tab[i-1].second + 1 < tab[i].first) { if (abs(s - (tab[i-1].second + 1)) < min_odl || (abs(s - (tab[i-1].second + 1)) == min_odl && tab[i-1].second + 1 < wynik)) { min_odl = abs(s - (tab[i - 1].second + 1)); wynik = tab[i - 1].second + 1; } if (abs(s - (tab[i].first - 1)) < min_odl || (abs(s - (tab[i].first - 1)) == min_odl && tab[i].first - 1 < wynik)) { min_odl = abs(s - (tab[i].first - 1)); wynik = tab[i].first - 1; } } } if (tab[m-1].second < n) { long long akt = tab[m-1].second + 1; long long akt_odl = abs(s - akt); if (akt_odl < min_odl || (akt_odl == min_odl && akt < wynik)) { min_odl = akt_odl; wynik = akt; } } cout << wynik; 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 | #include <bits/stdc++.h> using namespace std; pair<long long, long long> tab[1001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, s, min_odl = LLONG_MAX, wynik = -1;; int m; cin >> n >> m >> s; for (int i = 0; i < m; i++) { cin >> tab[i].first >> tab[i].second; } sort(tab, tab + m); if (tab[0].first > 1) { long long akt = tab[0].first - 1; long long akt_odl = abs(s - akt); if (akt_odl < min_odl || (akt_odl == min_odl && akt < wynik)) { min_odl = akt_odl; wynik = akt; } } for (int i = 1; i < m; i++) { if (tab[i-1].second + 1 < tab[i].first) { if (abs(s - (tab[i-1].second + 1)) < min_odl || (abs(s - (tab[i-1].second + 1)) == min_odl && tab[i-1].second + 1 < wynik)) { min_odl = abs(s - (tab[i - 1].second + 1)); wynik = tab[i - 1].second + 1; } if (abs(s - (tab[i].first - 1)) < min_odl || (abs(s - (tab[i].first - 1)) == min_odl && tab[i].first - 1 < wynik)) { min_odl = abs(s - (tab[i].first - 1)); wynik = tab[i].first - 1; } } } if (tab[m-1].second < n) { long long akt = tab[m-1].second + 1; long long akt_odl = abs(s - akt); if (akt_odl < min_odl || (akt_odl == min_odl && akt < wynik)) { min_odl = akt_odl; wynik = akt; } } cout << wynik; return 0; } |