CF 14B B. Young Photographer, 14 bphotographer
Among other things, Bob is keen on photography. Especially he likes to take pictures of sportsmen. That was the reason why he placed himself in positionX0 of a long straight racetrack and got ready to take pictures. But the problem was that not all the runners passed him. The total amount of sportsmen, training at that racetrack, equalsN. And each of them regularly runs distances within a particle segment of the racetrack, which is the same for each sportsman. For example, the first sportsman runs from positionA1 to positionB1, the second-fromA2B2
What is the minimum distance that Bob shoshould move to have a chance to take pictures of each sportsman? Bob can take a picture of a sportsman, if he stands within the segment that this sportsman covers on the racetrack.
Input
The first line of the input file contains integersNAndX0 (1 digit ≤ DigitNMemory ≤ memory 100; 0 memory ≤ memoryX0 rows ≤ memory 1000). The followingNLines contain pairs of integersAI, Bytes,BI(0 bytes ≤ bytesAI, Bytes,BILimit ≤ limit 1000;AI =BI).
Output
Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output-1.
Sample test (s) input
3 30 714 24 6
Output
1
My practice is to maintain the maximum value (maxn) and minimum value (minn) =-=, and finally determine that x0 is not in this range. If there is no need to change, the output is 0, output to the minimum value of maxn and minn.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n,x0;struct node{ int a,b;}s[110];int cmp(node l1,node l2){ return l1.a<l2.a;}int main(){ int u,v,i; while(cin>>n>>x0) { for(i=0;i<n;i++) { cin>>u>>v; if(u>v) swap(u,v); s[i].a=u; s[i].b=v; } sort(s,s+n,cmp); int maxn=s[0].b; int minn=s[0].a; for(i=1;i<n;i++) { if(s[i].b<minn) break; if(s[i].a>maxn) break; if(s[i].a>minn) minn=s[i].a; if(s[i].b<maxn) maxn=s[i].b; } if(i==n) { if(x0>=minn&&x0<=maxn) cout<<0<<endl; else cout<<min(abs(minn-x0),abs(maxn-x0))<<endl; } else cout<<-1<<endl; } return 0;}
D: \ KuGou \ Lyric \ Dj-dance music-dynamic English voice-da0b2a1f1f28016b669c02973cf14b01krc name
SMS-Befour