Soluție HackerRank pentru Most Distant, subdomeniul Fundamentals, în C++14. Include cerința formatată, exemple, explicația pașilor și cod sursă.

  • Problemă: Most Distant
  • Domeniu: Fundamentals
  • Limbaj: C++14

Challenge: Most Distant

Subdomeniu: Fundamentals (fundamentals)

Scor cont: 30.0 / 30

Submission status: Accepted

Submission score: 1.0

Submission ID: 464726522

Limbaj: cpp14

Link challenge: https://www.hackerrank.com/challenges/most-distant/problem

Cerință

Keko has N dots in a 2-D coordinate plane. He wants to measure the gap between the most distant two dots. To make the problem easier, Keko decided to change each dot's x *or* y coordinate to zero.

Help Keko calculate the distance!

Input Format

The first line contains an integer, N, the number of dots.
The next N lines each contain the integer coordinates of the dots in (x, y) fashion.

Constraints
2≤ N ≤ 10^6
-10^9≤ x_i,,y_i ≤ 10^9

It is guaranteed that all dots are distinct, and either their x or y coordinate is equal to 0.

Output Format

Print the distance between the most distant dots with an absolute error of, at most, 10^-6.

Cod sursă

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    double mnx,mxx,mny,mxy; //min_x_axis  max_x_axis  min_y_axis  max_y_axis  
    int t;
    cin>>t;
    while(t--)
    {
        double a,b;
        cin>>a>>b;
        if(a==0)
        {
            mny=min(mny,b);
            mxy=max(mxy,b);
        }
        else
        {
            mnx=min(mnx,a);
            mxx=max(mxx,a);
            
        }
        
        
    }
        double mx=max(abs(mxx-mnx),abs(mny-mxy));//axis
        double q1=sqrt(mxx*mxx+mxy*mxy); //quadrant 1
        double q2=sqrt(mnx*mnx+mxy*mxy);    //2
        double q3=sqrt(mnx*mnx+mny*mny);    //3
        double q4=sqrt(mxx*mxx+mny*mny);    //4
    
        printf("%.10f",max(mx,max(q1,max(q2,max(q3,q4)))));
    
    return 0;
}
HackerRank Fundamentals – Most Distant