Advertisement
tepyotin2

The Closest Pair Easy

Jun 27th, 2025
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Coor{
  6.     double x, y;
  7.     //double or else time limit exceeded
  8.     bool operator<(const Coor &a) const{
  9.         return x<a.x;
  10.     }
  11. };
  12.  
  13. int main(){
  14.     //freopen("closestpairproblem.in", "r", stdin);
  15.    
  16.     int n;
  17.     while(true){
  18.         cin >> n;
  19.         if(n == 0) return 0;
  20.         vector<Coor> coor(n);
  21.         for(int i=0; i<n; i++){
  22.             cin >> coor[i].x >> coor[i].y;
  23.         }
  24.         sort(coor.begin(), coor.end());
  25.         double mndist = INT_MAX;
  26.         for(int i=0; i<n-1; i++){
  27.             for(int j=i+1; j<n; j++){
  28.                 double dist;
  29.                 //cout << "coor[i].x: "<< coor[i].x << ", coor[i].y: " << coor[i].y
  30.                     //<< ", coor[j].x: " << coor[j].x << ", coor[j].y: " << coor[j].y << '\n';
  31.                 dist = (abs(coor[j].x-coor[i].x)*abs(coor[j].x-coor[i].x))
  32.                     +(abs(coor[j].y-coor[i].y)*abs(coor[j].y-coor[i].y));
  33.                 if(dist<mndist){
  34.                     mndist = dist;
  35.                 }
  36.             }
  37.         }
  38.         double ans = sqrt(mndist);
  39.         if(ans>10000){
  40.             cout << "INFINITY" << '\n';
  41.         }else{
  42.             cout << fixed << setprecision(4) << ans << '\n';
  43.             //fixed not just setprecision to 6 otherwise wrong answer
  44.         }
  45.     }
  46.    
  47.     return 0;
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement