#include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define all(x) (x).begin(), (x).end() #define BOOST ios_base::sync_with_stdio(0), cin.tie(0) typedef long long ll; typedef long double ld; typedef pair<int, int> ii; int main(){ BOOST; ll n, s; int m; cin >> n >> m >> s; vector<pair<ll, ll>> t(m); vector<ll> ans; for(int i=0; i<m; i++){ cin >> t[i].st >> t[i].nd; } sort(all(t)); if(t[0].st > 1) ans.pb(t[0].st - 1); for(int i=0; i<m; i++){ if(i > 0 && t[i-1].nd + 1 < t[i].st){ ans.pb(t[i].st-1); } if(i < m-1 && t[i].nd + 1 < t[i+1].st){ ans.pb(t[i].nd+1); } } if(t[m-1].nd < n) ans.pb(t[m-1].nd + 1); ll best = ans[0]; for(int i=1; i<ans.size(); i++){ if(abs(s - ans[i]) < abs(s - best)){ best = ans[i]; } } cout << best << "\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 41 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second #define pb push_back #define all(x) (x).begin(), (x).end() #define BOOST ios_base::sync_with_stdio(0), cin.tie(0) typedef long long ll; typedef long double ld; typedef pair<int, int> ii; int main(){ BOOST; ll n, s; int m; cin >> n >> m >> s; vector<pair<ll, ll>> t(m); vector<ll> ans; for(int i=0; i<m; i++){ cin >> t[i].st >> t[i].nd; } sort(all(t)); if(t[0].st > 1) ans.pb(t[0].st - 1); for(int i=0; i<m; i++){ if(i > 0 && t[i-1].nd + 1 < t[i].st){ ans.pb(t[i].st-1); } if(i < m-1 && t[i].nd + 1 < t[i+1].st){ ans.pb(t[i].nd+1); } } if(t[m-1].nd < n) ans.pb(t[m-1].nd + 1); ll best = ans[0]; for(int i=1; i<ans.size(); i++){ if(abs(s - ans[i]) < abs(s - best)){ best = ans[i]; } } cout << best << "\n"; } |