Wrong answer in problem POINTS

Hello @all,

I was trying to solve this problem: [POINTS][1].

I think Im accounting for all the I/O format specifications and problem statement… However, Im getting WA… Can you help me out?

Here’s my code:

   #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

struct Point
{
	int x;
	int y;
};

double dist(Point p1, Point p2)
{
	return sqrt( pow((p1.x-p2.x),2) + pow((p1.y-p2.y),2));
}

bool comp(Point a, Point b)
{
	if(a.x < b.x)
	return (a.x < b.x);
	else if(b.x < a.x)
	return (b.x > a.x);
	else if(a.x==b.x and a.y > b.y)
	return (a.y > b.y);
	else if(a.x==b.x and b.y > a.y)
	return (b.y > a.y);
}

int main()
{
	int t;
	scanf("%d", &t);
	for(int i = 0; i < t; i++)
	{
		cout << endl;
		int num;
		scanf("%d", &num);
		Point array[num];
		for(int j = 0; j < num; j++)
		{
			int abx, ord;
			scanf("%d %d", &abx, &ord);
			array[j].x=abx;
			array[j].y=ord;
		}
		double distance = 0.00;
		sort(array,array+num,comp);
		for(int i = 0; i < num-1; i++)
		{
			distance += dist(array[i],array[i+1]);
		}
		cout << endl;
		printf("%.2lf\n", distance);
	}
	return 0;
}

Thanks in advance,

Bruno
[1]: http://www.codechef.com/problems/POINTS

1 Like

if(a.x < b.x)
return (a.x < b.x);
else
return (b.x > a.x);

After these four lines in your comp function other lines are not reachable , because if the condition is true then first return statement will be executed otherwise second return statement will be executed .
The checks for cases when x coordinate is equal are hence not part of your comp function .

1 Like

Thanks for pointing this out… I have fixed it, but still get WA!

bool comp(Point a, Point b)
{
if(a.x < b.x)
return (a.x < b.x);
else if(b.x < a.x)
return (b.x > a.x);
else if(a.x==b.x and a.y > b.y)
return (a.y > b.y);
else if(a.x==b.x and b.y > a.y)
return (b.y > a.y);
}

This is your new comp function, right ?? Had to really search your submission. Please always give a link to the page where I can see your submitted code.

Anyways, If a.x == b.x then if a.y > b.y you return true and a.y < b.y then also you return true .
This is wrong. Just write

if(a.x!=b.x) 
    return a.x < b.x;
return a.y > b.y;

Thank you very much @vineetpaliwal,

I got AC after your correction :smiley:

And thank you, @anton for fixing code tags as well :smiley:

Thanks,

Bruno