#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (b); i >= (a); i--)
#define SZ(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using ll = long long;
using lf = long double;
using pii = pair <int,int>;
using pll = pair<ll, ll>;
auto& operator<<(auto& o, pair<auto, auto> p) {
return o << "(" << p.first << ", " << p.second << ")";
}
auto operator<<(auto& o, auto x)->decltype(end(x), o) {
o << "{"; int i = 0; for (auto e: x) o << ", " + 2*!i++ << e;
return o << "}";
}
#define dbg(x...) cerr<<"["#x"]: ", [](auto...$) {((cerr << $ << "; "),...)<<endl;}(x)
const int mxM = 1007, INF = 1e18;
pair<int,int> a[mxM];
int n, m;
bool sprawdz(int x){
if(x < 1 || x > n) return 0;
rep(i,1,m) if(a[i].st <= x && x <= a[i].nd) return 0;
return 1;
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int s, k; cin >> n >> m >> s;
rep(i,1,m) cin >> a[i].st >> a[i].nd;
int res = INF, id = 0;
rep(i,1,m){
k = a[i].st - 1;
if(sprawdz(k) && res > abs(s-k)){
res = abs(s-k);
id = k;
}
k = a[i].nd + 1;
if(sprawdz(k) && res > abs(s-k)){
res = abs(s-k);
id = k;
}
}
k = s-1;
if(sprawdz(k)){
if(res > abs(s-k) || (res == abs(s-k) && k < id)){
res = abs(s-k);
id = k;
}
}
k = s+1;
if(sprawdz(k)){
if(res > abs(s-k) || (res == abs(s-k) && k < id)){
res = abs(s-k);
id = k;
}
}
cout << id;
}
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 | #pragma GCC optimize ("O3") #include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (b); i >= (a); i--) #define SZ(x) ((int)x.size()) #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define st first #define nd second using ll = long long; using lf = long double; using pii = pair <int,int>; using pll = pair<ll, ll>; auto& operator<<(auto& o, pair<auto, auto> p) { return o << "(" << p.first << ", " << p.second << ")"; } auto operator<<(auto& o, auto x)->decltype(end(x), o) { o << "{"; int i = 0; for (auto e: x) o << ", " + 2*!i++ << e; return o << "}"; } #define dbg(x...) cerr<<"["#x"]: ", [](auto...$) {((cerr << $ << "; "),...)<<endl;}(x) const int mxM = 1007, INF = 1e18; pair<int,int> a[mxM]; int n, m; bool sprawdz(int x){ if(x < 1 || x > n) return 0; rep(i,1,m) if(a[i].st <= x && x <= a[i].nd) return 0; return 1; } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int s, k; cin >> n >> m >> s; rep(i,1,m) cin >> a[i].st >> a[i].nd; int res = INF, id = 0; rep(i,1,m){ k = a[i].st - 1; if(sprawdz(k) && res > abs(s-k)){ res = abs(s-k); id = k; } k = a[i].nd + 1; if(sprawdz(k) && res > abs(s-k)){ res = abs(s-k); id = k; } } k = s-1; if(sprawdz(k)){ if(res > abs(s-k) || (res == abs(s-k) && k < id)){ res = abs(s-k); id = k; } } k = s+1; if(sprawdz(k)){ if(res > abs(s-k) || (res == abs(s-k) && k < id)){ res = abs(s-k); id = k; } } cout << id; } |
English