#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n,m,s; int RESULT; cin >> n; cin >> m; cin >> s; vector<int> l(m); vector<int> r(m); for(int i=0;i<m;i++) { cin >> l[i]; cin >> r[i]; } sort(l.begin(),l.end()); sort(r.begin(),r.end()); int L = 0; int R= 0; int LEFT; int RIGHT; while(r[R] < s) { L++; R++; } int L_save = L; int R_save = R; while((r[R-1] == l[L]-1) && R >=1) { L--; R--; } if(l[L] != 1) { LEFT = l[L]-1; } else { LEFT = 0; } L = L_save; R = R_save; while((l[L+1] == r[R]+1) && L <= m-1) { L++; R++; } if(r[R] != n) { RIGHT = r[R]+1; } else { RIGHT = 0; } if(LEFT == 0) { RESULT = RIGHT; } else if(RIGHT == 0) { RESULT = LEFT; } else { if( (s-LEFT) <= (RIGHT-s)) { RESULT = LEFT; } else { RESULT = RIGHT; } } cout << RESULT; }
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n,m,s; int RESULT; cin >> n; cin >> m; cin >> s; vector<int> l(m); vector<int> r(m); for(int i=0;i<m;i++) { cin >> l[i]; cin >> r[i]; } sort(l.begin(),l.end()); sort(r.begin(),r.end()); int L = 0; int R= 0; int LEFT; int RIGHT; while(r[R] < s) { L++; R++; } int L_save = L; int R_save = R; while((r[R-1] == l[L]-1) && R >=1) { L--; R--; } if(l[L] != 1) { LEFT = l[L]-1; } else { LEFT = 0; } L = L_save; R = R_save; while((l[L+1] == r[R]+1) && L <= m-1) { L++; R++; } if(r[R] != n) { RIGHT = r[R]+1; } else { RIGHT = 0; } if(LEFT == 0) { RESULT = RIGHT; } else if(RIGHT == 0) { RESULT = LEFT; } else { if( (s-LEFT) <= (RIGHT-s)) { RESULT = LEFT; } else { RESULT = RIGHT; } } cout << RESULT; } |