#include <bits/stdc++.h> using namespace std; map <int, int> odc_l; map <int, int> odc_p; int rekurencja(int ind, auto& mp, int dodaj){ if (mp.find(ind)==mp.end()){ return ind; } return rekurencja(mp[ind]+dodaj, mp, dodaj); } int main() { int n, m, s; cin>>n>>m>>s; int best_left=s-1; int best_right=s+1; for (int i=0; i<m; i++){ int l, p; cin>>l>>p; odc_p[l]=p; odc_l[l]=l; odc_l[p]=l; odc_p[p]=p; if (best_left<=p && best_left>=l){best_left=rekurencja(l-1, odc_l, -1);} if (best_right<=p && best_right>=l){best_right=rekurencja(p+1, odc_p, 1);} //cout<<best_left<<" "<<best_right<<"\n"; //cout<<best_left<<" "<<best_right<<"\n"; } if (best_left<=0){cout<<best_right; return 0;} if (best_right>n){cout<<best_left; return 0;} if (fabs(s - best_left)>fabs(s - best_right)){cout<<best_right; return 0;} cout<<best_left; 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 | #include <bits/stdc++.h> using namespace std; map <int, int> odc_l; map <int, int> odc_p; int rekurencja(int ind, auto& mp, int dodaj){ if (mp.find(ind)==mp.end()){ return ind; } return rekurencja(mp[ind]+dodaj, mp, dodaj); } int main() { int n, m, s; cin>>n>>m>>s; int best_left=s-1; int best_right=s+1; for (int i=0; i<m; i++){ int l, p; cin>>l>>p; odc_p[l]=p; odc_l[l]=l; odc_l[p]=l; odc_p[p]=p; if (best_left<=p && best_left>=l){best_left=rekurencja(l-1, odc_l, -1);} if (best_right<=p && best_right>=l){best_right=rekurencja(p+1, odc_p, 1);} //cout<<best_left<<" "<<best_right<<"\n"; //cout<<best_left<<" "<<best_right<<"\n"; } if (best_left<=0){cout<<best_right; return 0;} if (best_right>n){cout<<best_left; return 0;} if (fabs(s - best_left)>fabs(s - best_right)){cout<<best_right; return 0;} cout<<best_left; return 0; } |