Advertisement
YEZAELP

CUBE-133: Xmas Beam

Oct 23rd, 2020
115
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int INF = 2e9;
  5. using pi = pair <int, int>;
  6.  
  7. bool compare(pi a, pi b){
  8.     return a.second < b.second;
  9. }
  10.  
  11. int f(int n, int H){
  12.     pi ar[n+1];
  13.     for(int i=1;i<=n;i++){
  14.         scanf("%d%d", &ar[i].first, &ar[i].second);
  15.     }
  16.     sort(ar + 1, ar + n + 1, compare);
  17.     int mn = INF;
  18.     for(int i=1;i<=n;i++){
  19.         for(int j=1;j<i;j++){
  20.             if(abs(ar[j].second - ar[i].second) < H) break;
  21.             mn = min(mn, abs(ar[i].first - ar[j].first));
  22.         }
  23.     }
  24.     if(mn == INF) return -1;
  25.     return mn;
  26. }
  27.  
  28. int main(){
  29.  
  30.     int Q;
  31.     scanf("%d", &Q);
  32.  
  33.     while(Q--){
  34.         int n, H;
  35.         scanf("%d%d", &n, &H);
  36.         printf("%d\n", f(n, H));
  37.     }
  38.  
  39.     return 0;
  40. }
  41.  
Advertisement
RAW Paste Data Copied
Advertisement