#include <bits/stdc++.h>
using namespace std;
pair<long long,long long> tab[1009];
long long ind,wyn1,wyn2,wyn;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n,m,s;
cin>>n>>m>>s;
for(int i=0;i<m;i++){
cin>>tab[i].first>>tab[i].second;
}
sort(tab,tab+m);
for(int i=0;i<m;i++){
// cout<<tab[i].first<<" "<<tab[i].second<<endl;
}
for(int i=0;i<m;i++){
if(tab[i].first<=s && tab[i].second>=s){
ind=i;
break;
}
}
//cout<<ind;
for(int i=ind;i>=0;i--){
if(tab[i].first-1!=tab[i-1].second && tab[i].first-1>0){
wyn1=tab[i].first-1;
break;
}
}
for(int i=ind;i<n-1;i++){
if(tab[i].second+1!=tab[i+1].first && tab[i].second+1<=n){
wyn2=tab[i].second+1;
break;
}
}
// cout<<wyn1<<wyn2;
if(wyn1==0){
cout<<wyn2;
return 0;
}
if(wyn2==0){
cout<<wyn1;
return 0;
}
// cout<<wyn1<<" "<<wyn2;
if(abs(s-wyn2)< abs(s-wyn1)){
cout<<wyn2;
return 0;
}
if(abs(s-wyn2)>= abs(s-wyn1)){
cout<<wyn1;
return 0;
}
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 | #include <bits/stdc++.h> using namespace std; pair<long long,long long> tab[1009]; long long ind,wyn1,wyn2,wyn; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n,m,s; cin>>n>>m>>s; for(int i=0;i<m;i++){ cin>>tab[i].first>>tab[i].second; } sort(tab,tab+m); for(int i=0;i<m;i++){ // cout<<tab[i].first<<" "<<tab[i].second<<endl; } for(int i=0;i<m;i++){ if(tab[i].first<=s && tab[i].second>=s){ ind=i; break; } } //cout<<ind; for(int i=ind;i>=0;i--){ if(tab[i].first-1!=tab[i-1].second && tab[i].first-1>0){ wyn1=tab[i].first-1; break; } } for(int i=ind;i<n-1;i++){ if(tab[i].second+1!=tab[i+1].first && tab[i].second+1<=n){ wyn2=tab[i].second+1; break; } } // cout<<wyn1<<wyn2; if(wyn1==0){ cout<<wyn2; return 0; } if(wyn2==0){ cout<<wyn1; return 0; } // cout<<wyn1<<" "<<wyn2; if(abs(s-wyn2)< abs(s-wyn1)){ cout<<wyn2; return 0; } if(abs(s-wyn2)>= abs(s-wyn1)){ cout<<wyn1; return 0; } return 0; } |
English