#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i= 0; i<(n); i++) #define reps(i,s, n) for(int i= (s); i<(n); i++) #define each(a, x) for (auto &a : x) #define vv(T) vector<T> #define endl '\n' #define sz(x) (int)x.size() #define ll long long #define all(c) begin(c), end(c) #define fi first #define se second #define mp make_pair #define pb push_back #define pd pair<int,int> #define wr cout<< #define wre wr endl; #define wrut(a) {each(i,(a))wr i<<" "; wre} #define wrot(a,b,c) {wre wr a<<" "<<b<<" "<<c<<" ";wre} #define int ll const int inf =1e14+7; bool comp(pd p1,pd p2){ return p1.se<p2.se; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m,s; int p=-inf,p1=inf; cin>>n>>m>>s; vv(pd) a(m); rep(i,m){ cin>>a[i].fi>>a[i].se; } sort(all(a),comp); if(a[0].fi-1>0)p=a[0].fi-1; if(a[m-1].se+1<=n)p1=a[m-1].se+1; reps(i,1,m){ if(a[i-1].se<a[i].fi-1){ if(s>a[i-1].se+1)p=a[i-1].se+1; else if(a[i-1].se+1<p1)p1=a[i-1].se+1; if(s>a[i].fi-1)p=a[i].fi-1; else if(a[i].fi-1<p1)p1=a[i].fi-1; } // cout<<a[i].fi<<" "<<a[i].se<<endl; } // wr p<<" "<<p1<<endl; if(s-p<=p1-s){ wr p; }else{ wr p1; } }
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 | #include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i= 0; i<(n); i++) #define reps(i,s, n) for(int i= (s); i<(n); i++) #define each(a, x) for (auto &a : x) #define vv(T) vector<T> #define endl '\n' #define sz(x) (int)x.size() #define ll long long #define all(c) begin(c), end(c) #define fi first #define se second #define mp make_pair #define pb push_back #define pd pair<int,int> #define wr cout<< #define wre wr endl; #define wrut(a) {each(i,(a))wr i<<" "; wre} #define wrot(a,b,c) {wre wr a<<" "<<b<<" "<<c<<" ";wre} #define int ll const int inf =1e14+7; bool comp(pd p1,pd p2){ return p1.se<p2.se; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m,s; int p=-inf,p1=inf; cin>>n>>m>>s; vv(pd) a(m); rep(i,m){ cin>>a[i].fi>>a[i].se; } sort(all(a),comp); if(a[0].fi-1>0)p=a[0].fi-1; if(a[m-1].se+1<=n)p1=a[m-1].se+1; reps(i,1,m){ if(a[i-1].se<a[i].fi-1){ if(s>a[i-1].se+1)p=a[i-1].se+1; else if(a[i-1].se+1<p1)p1=a[i-1].se+1; if(s>a[i].fi-1)p=a[i].fi-1; else if(a[i].fi-1<p1)p1=a[i].fi-1; } // cout<<a[i].fi<<" "<<a[i].se<<endl; } // wr p<<" "<<p1<<endl; if(s-p<=p1-s){ wr p; }else{ wr p1; } } |