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
#include <stdio.h>
#include <stdlib.h>


struct pair{
long long int x;
long long int y;
};

int compare( const void *a, const void *b )
{
   if( (*( struct pair *)a).x > (*( struct pair *)b).x) return 1;
   if( (*( struct pair *)a).x < (*( struct pair *)b).x) return -1;
   return 0;
}

int main()
{

struct pair tab[1002];

int i,m;
long long int n,s,x,y,max=3000000000000;
scanf("%lld %d %lld",&n,&m,&s);
for(i=1;i<=m;i++)
{
 scanf("%lld %lld",&x,&y);
 tab[i].x=x;
 tab[i].y=y;
}
tab[m+1].x=n+1;
tab[0].x=0;
tab[0].y=0;

qsort(tab,m+2,sizeof(struct pair),compare);

for(i=0;i<=m;i++)
{
 if(tab[i].y+1>=tab[i+1].x) continue;
 if(abs(tab[i].y+1-s) < abs(max-s)) max=tab[i].y+1;
 if(abs(tab[i+1].x-1-s) < abs(max-s)) max=tab[i+1].x-1;
}

printf("%lld\n",max);
return 0;
}