#include<set>
#include<map>
#include<queue>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
#define pr pair
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pll pr<ll,ll>
#define pii pr<ll,ll>
#define piii pr<int,pii>
using namespace std;
pii p[1003];
pii calc(ll l,ll r,ll s)
{
if(l==r) return mp(1ll<<60,-1);
if(s>=l&&s<r) return mp(0,s);
return min(mp(abs(s-l),l),mp(abs(s-r+1),r-1));
}
int main()
{
ios_base::sync_with_stdio(0);
ll n,s;
int m;
cin>>n>>m>>s;
s--;
for(int i=0;i<m;i++) cin>>p[i].f>>p[i].s,p[i].f--;
sort(p,p+m);
pii mi=mp(n+1,-1);
mi=min(mi,calc(0,p[0].f,s));
mi=min(mi,calc(p[m-1].s,n,s));
for(int i=1;i<m;i++) mi=min(mi,calc(p[i-1].s,p[i].f,s));
cout<<mi.s+1<<endl;
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 | #include<set> #include<map> #include<queue> #include<vector> #include<algorithm> #include<bits/stdc++.h> #define pr pair #define f first #define s second #define ll long long #define mp make_pair #define pll pr<ll,ll> #define pii pr<ll,ll> #define piii pr<int,pii> using namespace std; pii p[1003]; pii calc(ll l,ll r,ll s) { if(l==r) return mp(1ll<<60,-1); if(s>=l&&s<r) return mp(0,s); return min(mp(abs(s-l),l),mp(abs(s-r+1),r-1)); } int main() { ios_base::sync_with_stdio(0); ll n,s; int m; cin>>n>>m>>s; s--; for(int i=0;i<m;i++) cin>>p[i].f>>p[i].s,p[i].f--; sort(p,p+m); pii mi=mp(n+1,-1); mi=min(mi,calc(0,p[0].f,s)); mi=min(mi,calc(p[m-1].s,n,s)); for(int i=1;i<m;i++) mi=min(mi,calc(p[i-1].s,p[i].f,s)); cout<<mi.s+1<<endl; return 0; } |
English