The problem:
So basically, I tried to sort the given coordinates according to the following conditions:

Sort all x’s in the ascending order. Greater the x, farther away it is positioned in the queue.

During sorting, if the value of abscissa (x coordinate) of the two coordinates is same, then the coordinate having the smaller ordinate (ycoordinate) of the two gets pushed back in the queue.
(read the following example to understand it better)
Example:
If given inputs are : (0,0) (1,2) (1,4) (4,0)
The sorted input would be: (0,0) (1,4) (1,2) (4,0)
[Notice that (1.4) gets placed before (1,2) because 2 is smaller than 4]
ALGORITHM:
As you may have noticed, I basically used a revised version of bubblesort for this (in it’s crudest form: sort x’s in ascending order… if they are same, sort corresponding y’s in descending order). After sorting it, I have to find the distance between the starting and the ending coordinates (i.e. in my example, between (0,0) and (4,0))
So, all I have to do is find the distance between all adjacent coordinates (using basic forumla to find distance between 2 coordinates) and add them up to get the total distance from starting point to ending point.
BUT ALAS! I am getting WA Can anyone kindly review the code or algorithm:
#include<iostream>
using namespace std;
#define MAX 10002
#include<math.h>
#include<iomanip>
void bubblesort(int x[], int y[], int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<ni1;j++ )
if(x[j]>x[j+1] or (x[j]==x[j+1] and y[j]<y[j+1]))
{
int temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
temp=y[j];
y[j]=y[j+1];
y[j+1]=temp;
}
}
}
long double dbtp(int xo, int yo, int xt, int yt)
{
return sqrt( pow( (xoxt), 2) + pow((yoyt), 2) );
}
int main()
{
int t;
cin>>t;
while(t)
{
int n, x[MAX], y[MAX];
cin>>n;
for(int i=0;i<n;i++)
cin>>x[i]>>y[i];
bubblesort(x, y, n);
long double dist=0;
for(int i=0;i<n1;i++)
dist+=dbtp(x[i], y[i], x[i+1], y[i+1]); //distance between 2 coordinates
cout << setprecision(2) << fixed; // to ensure I get upto 2 decimal places
cout<< dist <<endl;
}
return 0;
}