#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll a,b,c; cin>>a>>b>>c; string t=""; for(ll i=0;i<b;i++){ ll d,e; cin>>d>>e; for(ll j=d;j<=e;j++){ t+=to_string(j); t+=","; } } ll index1=c-1,index2=c+1; while(index1>=0 && index2<=a && t.find(to_string(index1))!=string::npos && t.find(to_string(index2))!=string::npos){ index1--; index2++; } if(index1==-1){ while(t.find(to_string(index2))!=string::npos)index2++; cout<<index2; } else if(index2==a+1){ while(t.find(to_string(index1))!=string::npos)index1--; cout<<index1; } else{ if(t.find(to_string(index1))==string::npos){cout<<index1;} else{cout<<index2;} }}
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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll a,b,c; cin>>a>>b>>c; string t=""; for(ll i=0;i<b;i++){ ll d,e; cin>>d>>e; for(ll j=d;j<=e;j++){ t+=to_string(j); t+=","; } } ll index1=c-1,index2=c+1; while(index1>=0 && index2<=a && t.find(to_string(index1))!=string::npos && t.find(to_string(index2))!=string::npos){ index1--; index2++; } if(index1==-1){ while(t.find(to_string(index2))!=string::npos)index2++; cout<<index2; } else if(index2==a+1){ while(t.find(to_string(index1))!=string::npos)index1--; cout<<index1; } else{ if(t.find(to_string(index1))==string::npos){cout<<index1;} else{cout<<index2;} }} |