#include <bits/stdc++.h>
using namespace std;
#define _DEBUG
#ifdef _DEBUG
template<typename T1, typename T2> auto& operator<<(ostream& o, pair<T1, T2> a) { return o << "(" << a.first << ", " << a.second << ")"; }
template<typename T, typename OS> auto& operator<<(OS& o, T a) { o << "{"; for (auto b : a) o << b << ", "; return o << "}"; }
#define dbg(x...) cerr << "[" #x "]: ", [](auto... args) { ((cerr << args << ", "),...) << "\n"; }(x)
#else
#define dbg(...)
#endif
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define db cerr << "\033[91m"
using ll = long long;
using ld = long double;
using pll = pair<ll,ll>;
using vi = vector<int>;
int main() {
cin.tie(0)->sync_with_stdio(0);
ll n, m, s;
cin >> n >> m >> s;
vector<pll> p(m);
for(ll i = 0 ; i < m ; ++i) cin >> p[i].F >> p[i].S;
sort(all(p));
ll ans = -1;
for(ll i = 0 ; i < m ; ++i) {
if((i == 0 || p[i-1].S != p[i].F-1) && p[i].F != 1) {
ll x = p[i].F-1;
if(ans == -1 || abs(x-s) < abs(ans-s)) ans = x;
}
if((i == n-1 || p[i+1].F != p[i].S+1) && p[i].S != n) {
ll x = p[i].S+1;
if(ans == -1 || abs(x-s) < abs(ans-s)) ans = x;
}
}
cout << ans << '\n';
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 | #include <bits/stdc++.h> using namespace std; #define _DEBUG #ifdef _DEBUG template<typename T1, typename T2> auto& operator<<(ostream& o, pair<T1, T2> a) { return o << "(" << a.first << ", " << a.second << ")"; } template<typename T, typename OS> auto& operator<<(OS& o, T a) { o << "{"; for (auto b : a) o << b << ", "; return o << "}"; } #define dbg(x...) cerr << "[" #x "]: ", [](auto... args) { ((cerr << args << ", "),...) << "\n"; }(x) #else #define dbg(...) #endif #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define F first #define S second #define db cerr << "\033[91m" using ll = long long; using ld = long double; using pll = pair<ll,ll>; using vi = vector<int>; int main() { cin.tie(0)->sync_with_stdio(0); ll n, m, s; cin >> n >> m >> s; vector<pll> p(m); for(ll i = 0 ; i < m ; ++i) cin >> p[i].F >> p[i].S; sort(all(p)); ll ans = -1; for(ll i = 0 ; i < m ; ++i) { if((i == 0 || p[i-1].S != p[i].F-1) && p[i].F != 1) { ll x = p[i].F-1; if(ans == -1 || abs(x-s) < abs(ans-s)) ans = x; } if((i == n-1 || p[i+1].F != p[i].S+1) && p[i].S != n) { ll x = p[i].S+1; if(ans == -1 || abs(x-s) < abs(ans-s)) ans = x; } } cout << ans << '\n'; return 0; } |
English