YEZAELP

CUBE-103: Lock On!

Jun 23rd, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int INF=1e9;
  4. using pii=pair<int,int>;
  5. int main(){
  6.     int n,p;
  7.     scanf("%d%d",&n,&p);
  8.     vector <pii> g;
  9.     for(int i=0;i<n;i++){
  10.         int a,b,k;
  11.         scanf("%d%d",&a,&b);
  12.         if(a>b) {
  13.             k=a;
  14.             a=b;
  15.             b=k;
  16.         }
  17.         g.push_back({a,1});//add
  18.         g.push_back({b,2});//remove
  19.     }
  20.     sort(g.begin(),g.end());
  21.     int cnt=0,s=-1,e=-1;
  22.     for(auto i:g){
  23.         if(i.second==1){
  24.             cnt++;
  25.             if(cnt==n and s==-1) s=i.first;
  26.         }
  27.         else{
  28.             cnt--;
  29.             if(cnt==n-1 and s!=-1) e=i.first;
  30.         }
  31.     }
  32.     if(s==-1) printf("-1");
  33.     else if(s<=p and p<=e) printf("0");
  34.     else if(p<s) printf("%d",s-p);
  35.     else printf("%d",p-e);
  36.  
  37.     return 0;
  38. }
Add Comment
Please, Sign In to add comment