#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back vector<pair<ll,ll>> W; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n , k , s; cin>> n >> k >> s; ll a , b; ll cs =0 ; ll odp = 0 ; ll oldcs=1000000000000000; W.pb({0,0}); for(int i = 1 ; i<=k ; ++i) { cin >> a >> b; W.pb({a,b}); } sort(W.begin(),W.end()); W.pb({n+1,1000000000000000}); /*for( auto it : W) cout << it.first << " " << it.second << " ";*/ for( int i = 1 ; i <= k ; ++i) { if(W[i].first-W[i-1].second>1) { cs = abs((W[i].first-1)-s); if(cs<oldcs) odp = W[i].first - 1; //cout << " cs1 " << cs<<" "; oldcs = cs;} if(W[i+1].first-W[i].second>1) {cs = abs((W[i].second+1)-s); if(cs<oldcs) odp = W[i].second+1; //cout << " cs " << cs<<" "; oldcs=cs;} } cout << odp; 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 | #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back vector<pair<ll,ll>> W; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n , k , s; cin>> n >> k >> s; ll a , b; ll cs =0 ; ll odp = 0 ; ll oldcs=1000000000000000; W.pb({0,0}); for(int i = 1 ; i<=k ; ++i) { cin >> a >> b; W.pb({a,b}); } sort(W.begin(),W.end()); W.pb({n+1,1000000000000000}); /*for( auto it : W) cout << it.first << " " << it.second << " ";*/ for( int i = 1 ; i <= k ; ++i) { if(W[i].first-W[i-1].second>1) { cs = abs((W[i].first-1)-s); if(cs<oldcs) odp = W[i].first - 1; //cout << " cs1 " << cs<<" "; oldcs = cs;} if(W[i+1].first-W[i].second>1) {cs = abs((W[i].second+1)-s); if(cs<oldcs) odp = W[i].second+1; //cout << " cs " << cs<<" "; oldcs=cs;} } cout << odp; return 0; } |