You are not logged in. Please login at www.codechef.com to post your questions!

×

Wrong answer in problem POINTS

Hello @all,

I was trying to solve this problem: POINTS.

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

asked 21 Jan '13, 20:43

kuruma's gravatar image

3★kuruma
17.7k72143209
accept rate: 8%

edited 21 Jan '13, 21:12


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;

link

answered 21 Jan '13, 21:47

vineetpaliwal's gravatar image

6★vineetpaliwal
12.4k47107171
accept rate: 12%

edited 21 Jan '13, 22:35

anton_lunyov's gravatar image

6★anton_lunyov ♦
6.7k62119138

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 .

link

answered 21 Jan '13, 20:49

vineetpaliwal's gravatar image

6★vineetpaliwal
12.4k47107171
accept rate: 12%

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

(21 Jan '13, 21:08) kuruma3★

Thank you very much @vineetpaliwal,

I got AC after your correction :D

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

Thanks,

Bruno

link

answered 21 Jan '13, 23:03

kuruma's gravatar image

3★kuruma
17.7k72143209
accept rate: 8%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×38
×15

question asked: 21 Jan '13, 20:43

question was seen: 728 times

last updated: 21 Jan '13, 23:03