Sunday, 14 January 2018

915B-BROWSER

915B - BROWSER
My Approach : 
I considered all the positions of "pos"  but we need not do that coz the solution only depends on the position of a and b.
MY APPROACH
I focused on the position of "pos".So, we should still check for extreme(border) cases and I still cant guarantee that this works coz ive not implemented it :P

SOLVED :
This can simply be done by considering only the positions of a and b coz we can take abs value of distance irrespective of the position of c
and we have no need to check any border cases !!! (cool :p)

CODE : 

#define MIN(a, b) (a<b? a: b)
#define ABS(a) (a<0? -(a): a)
int main(){
int n, pos, l, r;
scanf("%d %d %d %d", &n, &pos, &l, &r);
int sec = 0;
if (l > 1 && r == n){
sec = ABS(pos - l) + 1;
}
else if (l == 1 && r < n){
sec = ABS(r - pos) + 1;
}
else if (l > 1 && r < n){
sec = MIN(ABS(pos-l), ABS(r-pos)) + 1;
sec += r - l + 1;
}
printf("%d\n", sec);
return 0;
}

No comments:

Post a Comment