POINTS: Getting wa

#include<stdio.h>
#include<math.h>
#define dist(x1,y1,x2,y2) sqrt(pow(((x2)-(x1)),2)+pow(((y2)-(y1)),2))
#define all© ©.begin(),©.end()
#define pb push_back
#include
#include
#include
#include
using namespace std;
struct sort_point
{
bool operator() (const pair<long,long> &x,const pair<long,long> &y)
{
if (x.first<y.first)
return false;
}
};
struct partial_sort_point
{
bool operator() (const pair<long,long> &x,const pair<long,long> &y)
{
if(x.first==y.first)
return (y.second<x.second);
}
};
int main()
{
//freopen(“input.txt”,“r”,stdin);
int test;
for(scanf("%d",&test);test>0;test–)
{
long n,temp,x,y,i=0;
float total_dist=0;
pair<long,long> temp_pair;
vector< pair<long,long> > v;
printf("\n");
scanf("%d",&n);
for(;n>0;–n)
{
scanf("%d%d",&x,&y);
temp_pair.first=x;
temp_pair.second=y;
v.pb(temp_pair);
}
sort(all(v),sort_point());
sort(all(v),partial_sort_point());

//cout<<" \n"<<"Sorted list"<<"\n";
        for(;i!=((long)v.size()-1);i++)
        {
           // cout<<v[i].first<<" "<<v[i].second<<"\n";
            total_dist+=dist(v[i].first,v[i].second,v[i+1].first,v[i+1].second);
        }
       printf("%.2f\n",total_dist);
    }
    return 0;
}

Two errors:

  1. Use double (always) over float as it makes you lose precision. Print using printf("%.2lf\n",total_dist);
  2. The sort functions are ambiguous. They do not entail all the cases. The else’s aren’t defined. Use something like this (in a single function):
struct sort_point
{
    bool operator() (const pair<long,long> &x,const pair<long,long> &y)
    {
        if (x.first!=y.first)
            return (x.first<y.first);
        else
            return (y.second<x.second);
    }
};
1 Like