#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll s;
bool comp(ll a, ll b){
if(abs(s - a) != abs(s - b)) return abs(s - a) < abs(s - b);
return a < b;
}
bool compare(pair<ll, ll> a, pair<ll, ll> b){
return a.second < b.second;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
ll n, m;
cin >> n >> m >> s;
vector<pair<ll, ll>> przedzialy(m);
for(ll i = 0; i < m; i++) cin >> przedzialy[i].first >> przedzialy[i].second;
sort(przedzialy.begin(), przedzialy.end(), compare);
vector<ll> v;
for(int i = 0; i < m; i++){
ll x1 = przedzialy[i].first - 1;
ll x2 = przedzialy[i].second + 1;
if(x1 <= n and x1 >= 1){
if(i == 0) v.push_back(x1);
else if(przedzialy[i - 1].second != x1) v.push_back(x1);
}
if(x2 <= n and x2 >= 1){
if(i == m - 1) v.push_back(x2);
else if(przedzialy[i + 1].first != x2) v.push_back(x2);
}
}
sort(v.begin(), v.end(), comp);
cout << v[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 | #include<bits/stdc++.h> #define ll long long using namespace std; ll s; bool comp(ll a, ll b){ if(abs(s - a) != abs(s - b)) return abs(s - a) < abs(s - b); return a < b; } bool compare(pair<ll, ll> a, pair<ll, ll> b){ return a.second < b.second; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); ll n, m; cin >> n >> m >> s; vector<pair<ll, ll>> przedzialy(m); for(ll i = 0; i < m; i++) cin >> przedzialy[i].first >> przedzialy[i].second; sort(przedzialy.begin(), przedzialy.end(), compare); vector<ll> v; for(int i = 0; i < m; i++){ ll x1 = przedzialy[i].first - 1; ll x2 = przedzialy[i].second + 1; if(x1 <= n and x1 >= 1){ if(i == 0) v.push_back(x1); else if(przedzialy[i - 1].second != x1) v.push_back(x1); } if(x2 <= n and x2 >= 1){ if(i == m - 1) v.push_back(x2); else if(przedzialy[i + 1].first != x2) v.push_back(x2); } } sort(v.begin(), v.end(), comp); cout << v[0]; } |
English