#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
LL n, m, s;
cin>>n>>m>>s;
vector<pair<LL, LL>> v(m);
for(auto &[a, b] : v)cin>>a>>b;
sort(v.begin(), v.end());
set<LL> ss;
for(auto [a, b] : v){ss.insert(a);ss.insert(b);}
LL odl = 1000000000000001LL;
LL id = 0;
for(auto [a, b] : v){
a--;b++;
if(a > 0 && ss.find(a) == ss.end()){
if(abs(s-a) < odl){
id = a;
odl = abs(s-a);
}else if(abs(s-a) == odl){
if(a < id)id=a;
}
}
if(b <= n && ss.find(b) == ss.end()){
if(abs(s-b) < odl){
id = b;
odl = abs(s-b);
}else if(abs(s-b) == odl){
if(b < id)id=b;
}
}
}
cout<<id<<"\n";
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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); LL n, m, s; cin>>n>>m>>s; vector<pair<LL, LL>> v(m); for(auto &[a, b] : v)cin>>a>>b; sort(v.begin(), v.end()); set<LL> ss; for(auto [a, b] : v){ss.insert(a);ss.insert(b);} LL odl = 1000000000000001LL; LL id = 0; for(auto [a, b] : v){ a--;b++; if(a > 0 && ss.find(a) == ss.end()){ if(abs(s-a) < odl){ id = a; odl = abs(s-a); }else if(abs(s-a) == odl){ if(a < id)id=a; } } if(b <= n && ss.find(b) == ss.end()){ if(abs(s-b) < odl){ id = b; odl = abs(s-b); }else if(abs(s-b) == odl){ if(b < id)id=b; } } } cout<<id<<"\n"; return 0; } |
English